2013-08-20 45 views
2

我正在用cocos2dx开发2D游戏,其中我还很新...在游戏内部,有一些UI元素我想组合在一起(我打算将它们组合在一起到CCLayer s)。例如,几个文本标签和精灵形成一个CStatBar这是一个CCLayer。这CStatBar将包括在其他各种CCLayerCocos2dx sublayers

我该怎么做?我创建了CStatBar类,然后在包含类的init()函数中,我CStatBar::create()和调用this->addChild(pStatBar)但是,statbar没有出现......是否有任何明显的事情我错过了?所有的职位都是正确的。谢谢!

编辑:

注: 1.子层ccTouchesBegan叫,但它不会呈现/可见 2.如何调整子层,使其只覆盖父层的部分区域?按说CStatBar应该只覆盖屏幕,而不是整个屏幕的顶部面积的10%......

回答

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层小,你可以将其初始化函数内这样做,像这样:

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); 
}