Jump to content

What is the best way to remove all sprites inside a container?


Ninjadoodle
 Share

Recommended Posts

Hi @enpu / others who might be able to help :)

I'm trying to remove all the sprites inside a container when clicking, and I'm using this code ...

for (var i=0; i<game.scene.mg.children.length; i++) {
    game.scene.mg.children[i].remove();
}

Only some sprites get removed tho. Any idea what I'm doing wrong?

Thanks in advance!

Link to comment
Share on other sites

https://www.panda2.io/docs/api/Container

Look at Container methods, there is removeAll ;)

And the reason why your code is skipping some sprites is that your for loop is going from start to end. You should do it from end to start:

for (var i = game.scene.mg.children.length - 1; i >= 0; i--) {
    game.scene.mg.children[i].remove();
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...