您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx --- Widget 加载 CCNode

2014-06-05 16:25 337 查看
如题。

Widget addChild()   与 addNode()  两个方法。

现在我要加载一个粒子特效进去,下图:

Widget* layout =  dynamic_cast<Widget*>(pRoomWidget[roomId]->getChildByTag(10));

CCParticleSystemQuad* particle = CCParticleSystemQuad::create("particleTexture.plist");
particle->setPosition(CCPointZero);
layout->addChild(particle);


最初,我是直接 使用 layout->addChild(particle);         Log中有一个断言失败的错误:下图是错误位置

void Widget::addChild(CCNode* child, int zOrder, int tag)
{
CCAssert(dynamic_cast<Widget*>(child) != NULL, "Widget only supports Widgets as children");
CCNode::addChild(child, zOrder, tag);
_widgetChildren->addObject(child);
}


后来换成 layout->addNode(particle);

加载成功,没有断言失败。但在删除掉的时候出错。。。

解决方法有两种:

1、使用addChild()加载,但中间需要间隔一层Widget,如图:

<span style="white-space:pre">	</span>CCParticleSystemQuad* particle = CCParticleSystemQuad::create("particleTexture.plist");
particle->setPosition(CCPointZero);
Widget* pNode = Widget::create();
pNode->setPosition(CCPointZero);
pNode->addNode(particle);
layout->addChild(pNode);


删除时使用   

<span style="white-space:pre">	</span>layout->removeAllChildren();


2、使用addNode()加载

<span style="white-space:pre">	</span>CCParticleSystemQuad* particle = CCParticleSystemQuad::create("particleTexture.plist");
particle->setPosition(CCPointZero);
layout->addNode(particle);


同样,删除时需注意改为 

<span style="white-space:pre">	</span> layout->removeAllNodes();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息