gpt4 book ai didi

cocos2d-x - Cocos2dx 子层

转载 作者:行者123 更新时间:2023-12-04 04:45:07 27 4
gpt4 key购买 nike

我正在用 cocos2dx 开发一个 2D 游戏,其中我还很新……在游戏中,我想将许多 UI 元素组合在一起(我打算将它们组合成 CCLayer s )。例如一些文本标签和 Sprite 形成一个 CStatBar这是一个 CCLayer .此 CStatBar然后将被列入各种其他CCLayer
我该怎么做?我创建了 CStatBar然后,在包含类的 init() 函数中,我 CStatBar::create()并调用 this->addChild(pStatBar)但是,statbar 没有出现……我错过了什么明显的东西吗?所有的位置都是正确的。谢谢!

编辑:

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

最佳答案

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

关于cocos2d-x - Cocos2dx 子层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18326795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com