arunkumar
arunkumar

Reputation: 67

How to remove a sprite permanently in Cocos2d-x

I am developing a game using cocos2d-x. I want to remove sprites permanently. I have two sprites & making collision between the sprites. When collision happens, i want to remove those sprites permanently. I am using the following code to making collision & remove sprites.

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

Upvotes: 0

Views: 4569

Answers (2)

GameDeveloper
GameDeveloper

Reputation: 1029

if you assign each of your Sprites a tag, you can then do removeChildByTag(tag);

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


this->removeChildByTag(99);

Upvotes: 0

Blisskarthik
Blisskarthik

Reputation: 1242

To remove the sprite you can use

 sprrand24.removeFromParentAndCleanup(true);

Upvotes: 2

Related Questions