如何在Cocos2d-x中永久移除精灵

How to remove a sprite permanently in Cocos2d-x

本文关键字:精灵 Cocos2d-x      更新时间:2023-10-16

我正在使用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);
   }
  }
}

要删除精灵,可以使用

 sprrand24.removeFromParentAndCleanup(true);

如果你为每个精灵分配一个tag,那么你就可以分配removeChildByTag(tag);

sprite->setTag(99); // i made this up

this->removeChildByTag(99);