Skip to main content
Participating Frequently
February 23, 2009
Question

removeChild problem

  • February 23, 2009
  • 3 replies
  • 322 views
Hi,
I have the following code to add some objects (Ball) in stage and to remove them...
var Balls:Array = new Array();
BtnA.addEventListener(MouseEvent.CLICK, AddBox);
function AddBox(ev) {
for (var i:uint = 0; i<3; i++) {
var Ball:MyBall = new MyBall();
Balls.push(Ball);
addChild(Ball)
Ball.name = "A"+i;
Ball.x = 100 +i*50
Ball.y = 100
}
}

BtnB.addEventListener(MouseEvent.CLICK, RemoveBox);
function RemoveBox(ev){
for (var i:uint = 0; i<3; i++) {
removeChild(Balls )
}
}
The first time I click the button A I add 3 balls in stage and with click in button B remove them...
The second time I click the button A and add 3 balls in stage again but with click in button B I can't remove them...
The error is " ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at demoArray_fla::MainTimeline/RemoveBox() "
Where I'm wrong?
This topic has been closed for replies.

3 replies

Ned Murphy
Legend
February 23, 2009
You're welcome. As long as the intention is to not allow more than three at a time it should be okay.
Petros121Author
Participating Frequently
February 23, 2009
Thanks Ned...
I put in function "RemoveBox" the following line...
Balls.length = 0;
So I reset the array Balls and All is O.K.
Ned Murphy
Legend
February 23, 2009
You need to empty the array of the removedChildren. As is, you are trying to remove what has already been removed

I hope when they redesign this forum that they do away with using [ i ] as an italics formatting device. It's too common an index for arrays and you can see what it does to your remove function to make a slight harder to understand.