c++ - 变量在 Cocos2d-x for Blackberry 中失去值(value)

标签 c++ cocos2d-x

我在 Blackberry Playbook 上使用 Cocos2d-x。我正在尝试将 jpeg 居中放置在屏幕上,但我用来获取屏幕大小的变量不知何故最终为 0。尽管我可以在加载和定位 jpeg 之前确认它是非零的。我的初始化函数中的代码如下:

bool MyScene::init() {

    if ( !CCLayer::init() ) {
        return false;
    }

    CCSize size = CCDirector::sharedDirector()->getWinSize();

    bg = CCSprite::create("2.jpg");
    bg->setPosition(ccp( size.width/2 , size.height/2 ));
    this->addChild(bg); 

    fg = CCSprite::create("hud2.png");
    fg->setPosition(ccp( size.width/2 , 25 ));
    this->addChild(fg);

    fg2 = CCSprite::create("hud.png");
    fg2->setPosition(ccp( size.width/2 , size.height - 30 ));
    this->addChild(fg2);

    std::stringstream stream;
    stream << "size is " << (size.width/2) << "and " (size.height/2);
    std::string outtext = stream.str();

    label = CCLabelTTF::create(outtext.c_str(), "DejaVuSansMono.ttf", 25, CCSizeMake(size.width, 25), kCCTextAlignmentCenter);
    label->setPosition(ccp( size.width/2 , 28 ));
    this->addChild(label);
}

如果我运行它,标签将打印出大小为 0 并且 jpeg 位于左下角而不是居中的中间位置。但是,如果我加载 png 而不是 jpeg,一切正常。更让我困惑的是,如果我将用“size”值填充字符串的代码移动到设置“size”的代码的正下方,一切正常。如果我将该代码放在其他任何地方,或者将其完全取出,“大小”(大概)会回到 0 并且 jpeg 不会居中。这个问题只发生在黑莓上,但似乎“大小”设置正确所以我不知道这是一个 cocos2d-x 错误。会不会有一些编译器怪癖把事情搞砸了,或者只是我遗漏了一些其他编译器更宽容的非常简单的错误?

为了进一步混淆事情,我有另一个应用程序,其中背景设置在一个名为 initBackground 的单独函数中,该函数从主 init 函数调用。在该应用程序中,即使我手动将位置设置为屏幕中间,背景也会定位错误。高度是正确的,但如果我将 x 位置设置为 160,它的行为就像我将其设置为 0 一样,并且只显示一半的图像。

我不介意深入研究 cocos2d-x 代码,但非常感谢任何关于从哪里开始寻找的提示。

最佳答案

我知道这是一篇旧帖子,但我会尝试提供一些建议,以防其他人遇到类似问题。根据您的问题,我想可能会影响图像定位的一个变量是 anchor 值。也许由于某种原因,您的 anchor 设置为与图像中心不同的值 (0.5,0.5)。

The anchorPoint has a value range of 0.0 to 1.0 (0% to 100%). If the anchorPoint is 0.5x0.5 (the default for nodes with textures) then the texture is centered on the node's position. If the anchorPoint is 0x0, then the texture's lower left corner aligns with the node's position. If the anchorPoint is 1x1 then the texture's upper right corner aligns with the node's position. This makes anchorPoint most popular when it comes to left, right, top or bottom align nodes. It is most often used to align text labels or to align screen elements with screen borders.

您可以在这里了解更多关于 anchor 的信息 http://www.koboldtouch.com/display/IDCAR/How+The+Anchor+Point+Works+And+What+To+Use+It+For

关于您的变量被重置为零,我想这些行可能存在一些问题.. std::stringstream stream; stream << "size is " << (size.width/2) << "and " (size.height/2); std::string outtext = stream.str();

Blockquote

我更像是一个 java 程序员,但我想应该有一种方法可以直接将 size.width 的值添加到您的标签并打印出来。

希望 anchorPoint 的建议有所帮助。

关于c++ - 变量在 Cocos2d-x for Blackberry 中失去值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15783433/

相关文章:

c++ - 具有成员函数的实例化对象,在将其成员函数传递给新线程时表现为未实例化

c++ - 使用 dlopen,我如何应对已加载的库文件的更改?

android - COCOS-2d-x : Idea to implement Level progress in games

android - 清空 "Classes"文件夹cocos2dx 3.0 eclipse

c++ - Windows C++ 多字节/单码

c++ - 将运算符>>重载到 C++ 中的字符缓冲区 - 我可以告诉流长度吗?

c++ - 声明一个结构并立即用 cv 限定符或存储持续时间说明符实例化它

cocos2d-x - :couldn't find the gcc toolchain

android - 无法生成keystore并发布cocos2dx项目

c++ - Action 后释放新对象