Search code examples
createjseaseljs

Remove Shapes With Specific Name form Stage in EaselJS


If I have shapes being added to the stage in a loop, like this:

fooShape = new createjs.Shape().set({name:"newShapes"});

What is the easiest way to have a function that removes all shapes with the "newShapes" name?

I've tried things like stage.removeChild(fooShape[i]); but I'll get "not defined".


Solution

  • The removeChild method only display objects as parameters. To remove all children with a specific name, you have to loop through the children, and remove any that match the criteria:

    for (var i=container.numChildren-1; i>=0; i--) {
        var child = container.getChildAt(i)
        if (child.name == "newShapes") {
            container.removeChild(child);
        }
    }
    

    Note that this sample loops through the children in reverse, since removing an item will cause the indexes to change during the loop.

    Hope that helps.