我正在使用cocos2d-x开发游戏。我想永久删除精灵。我有两个精灵&在精灵之间发生碰撞。碰撞发生时,我想永久删除这些精灵。我使用以下代码进行碰撞和放大删除精灵。
CCARRAY_FOREACH(_sprrand24, stwentyfour)
{
CCSize size=sprrand24->getContentSize();
CCSprite *sprrand24 = dynamic_cast<CCSprite*>(stwentyfour);
CCRect sprrand24Rect = CCRectMake(
sprrand24->getPosition().x - (size.width/2),
sprrand24->getPosition().y - (size.height/2),
size.width/2,
size.height/2);
CCARRAY_FOREACH(_sprrand25, stwentyfive)
{
CCSize size=sprrand25->getContentSize();
CCSprite *sprrand25 = dynamic_cast<CCSprite*>(stwentyfive);
CCRect sprrand25Rect = CCRectMake(
sprrand25->getPosition().x - (size.width/2),
sprrand25->getPosition().y - (size.height/2),
size.width/2,
size.height/2);
if (sprrand24Rect.intersectsRect(sprrand25Rect))
{
this->removeChild(sprrand24, true);
this->removeChild(sprrand25, true);
}
}
}
答案 0 :(得分:2)
要删除精灵,您可以使用
sprrand24.removeFromParentAndCleanup(true);
答案 1 :(得分:0)
如果您为每个精灵分配tag
,则可以removeChildByTag(tag);
sprite->setTag(99); // i made this up
this->removeChildByTag(99);