2

我正在用 cocos2dx 开发一个 2D 游戏,在其中我还很新……在游戏中,我想将许多 UI 元素组合在一起(我打算将它们组合成CCLayers)。例如,一些文本标签和 sprite 形成 aCStatBar这是 a CCLayer。然后,这CStatBar将包含在其他各种CCLayer

我怎么做?我创建了这个CStatBar类,然后,在包含类的 init() 函数中,我CStatBar::create()调用this->addChild(pStatBar)了但是,statbar 没有出现......我错过了什么明显的东西吗?所有的位置都是正确的。谢谢!

编辑:

注意: 1.ccTouchesBegan调用了子图层,但没有渲染/看到 2. 如何调整子图层的大小,使其仅覆盖父图层的部分区域?据说CStatBar应该只覆盖屏幕顶部区域的 10%,而不是整个屏幕......

4

1 回答 1

3

CParent::init()函数内部,您可以像这样初始化CSublayer

        // Create and initialize CSublayer
    CSublayer* pSublayer= CSublayer::create();
    float fWidth = pSublayer->getContentSize().width;
    float fHeight = pSublayer->getContentSize().height;
    pSublayer->setPosition(ccp(fWidth/2.0f, fHeight/2.0f));
    this->addChild( pSublayer );

并且您的 CSublayer 可以像其他 CCLayer 一样定义。

如果您想将 CSublayer 限制为小于 CParent 层,可以在其 init 函数中执行此操作,如下所示:

CSublayer::init() {
        // initialize the size with the size of the background sprite
    CCSprite *pSpriteBackground = CCSprite::createWithSpriteFrame(
        CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("background.png")
    );
    this->setContentSize(CCSize(pSpriteBackground->getContentSize().width, pSpriteBackground->getContentSize().height));
    pSpriteBackground->setPosition(ccp(fScreenHalfWidth, fScreenHeight-(pSpriteBackground->getContentSize().height/2.0f)));
    this->addChild(pSpriteBackground);
}
于 2013-08-20T04:43:03.720 回答