Question

I'm working on a game and ran into an issue: I don't know how to remove one Shape object from canvas/stage. For instance - in one example I'm adding shapes with this code:

var bullet = new createjs.Shape();
bullet.graphics.beginFill("white").drawRect(0, 0, playerSize/5, playerSize);
bullet.x = x+playerSize/2;
bullet.y = y-playerSize;
stage.addChild(bullet);

bullets.push(bullet);

The last line adds the shape object into an array, so I can manipulate every object later (moving bullets using Tick)

Now I want to remove certain object, but I am not sure how: i know that you can delete parts of the stage with stage.removeAllChildren(), but this is not what I want. I have a working collision detection for bullets, but don't know how to get rid of them on the canvas when I want to.

I also tried stage.removeChild(bullet); but this didn't seem to work ? And how would I use removeChild if I have these object in an array only ?

Any suggestions/comments are welcome! If I was not clear enough, please tell me to update the question :)

thank you

Was it helpful?

Solution

I figured it out; didn't know you can do this. I just used stage.removeChild(bullet) in a loop through all bullets like this:

stage.removeChild(bullets[i]);

Which is interesting, since i thought you have to pass child name into this method, but obviously I was wrong; you pass the actual object and that's it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top