当前位置: 动力学知识库 > 问答 > 编程问答 >

cocos2d-x removed child to be added again will not worked

问题描述:

scene->removeChild(sprite);//removeChild(sprite, false) and removeChild(sprite, true) all tried

scene->addChild(sprite);

the sprite will not be seen. What's happening?

网友答案:

It seems that your child is simply destroyed after being removed from the scene. In cocos2d-x there is a objective-c-like reference counting system, that will reduce reference count on this sprite after it's being removed from parent. If counter reaches 0 value, node will be destroyed. Make sure that your sprite is not null before call scene->addChild(sprite);

sprite->retain();
scene->removeChild(sprite);
scene->addChild(sprite);
sprite->release();

Every time you call retain by yourself, you have to call release after it to prevent leaks.

网友答案:

After removing sprite you need to retain, and after adding again you need to release.

sprite->retain();   
scene->removeChild(sprite);
scene->addChild(sprite);
sprite->release();
分享给朋友:
您可能感兴趣的文章:
随机阅读: