How to delete all sprites from scene

Hello developers! I want delete my all sprites from scene how can I do it?

How are you storing your sprites? In a vector? If so loop through and removeFromParentAndCleanup()

1 Like

I’m storing with this way:

square = Sprite::create("squareNormal.png");
square->setPosition(Vec2(touch->getLocation().x, touch->getLocation().y));

It’s deleting buttons when I run this code removeFromParentAndCleanup(true);
It’s work but I don’t want delete my buttons. I want delete only sprites.

I can suggest three options:

  1. store all sprites in your vector or list.
    When want to delete go through all sprites and call removeFromParentAndCleanup()

  2. When want to delete go through all childs of parent (scene?) and check is it sprite.
    If so call removeFromParentAndCleanup()
    To check is it sprite:

    if (dynamic_cast<Sprite*>(childNode))
    
  3. Add sprites to separate parent node.
    When want to delete go through all childs of parent node and call removeFromParentAndCleanup()

Thank you for reply! I will try your options.

You should choose one option :wink:

Ahaha :sweat_smile::sweat_smile::joy:

I always lean towards 1 mostly for my needs. I’m all for portable containers :slight_smile: