Hi, i’m making a mini-game (part of a larger game) where worms pop up randomly on the screen. You can click on a spraycan do remove them all at once.
After that it should repeat itself. But during the second wave, when I press my spraycan, it doesn’t work anymore and it gives me this error:
"ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at TerragotchiV2_fla::MainTimeline/dropPesticide() "
code
body.addEventListener(Event.ENTER_FRAME,addWorm);
var wormNum:Number = 0;
function addWorm(e:Event):void {
var randomWorm = Math.random();
if (randomWorm < 0.07) {
var badWorm:BadWorm = new BadWorm;
badWorm.scaleX = 0.6 ; badWorm.scaleY = 0.6;
badWorm.doubleClickEnabled = true;
badWormArray.push(badWorm);
badWormArray[wormNum].x = Math.random() * 400;
badWormArray[wormNum].y = Math.random() * 340 + 340;
badWormArray[wormNum].addEventListener(MouseEvent.DOUBLE_CLICK,hit);
addChildAt(badWorm,4);
wormNum += 1;
}
}
To remove I use this function:
for (var wormNum in badWormArray) {
removeChild(badWormArray[wormNum]);
}
badWormArray.splice(0);
wormNum = 0;
Also I noticed, for exmaple if 4 worms appeared the first time, I spray and remove them, I get this error 4 times: “TypeError: Error #1010: A term is undefined and has no properties.
at TerragotchiV2_fla::MainTimeline/addWorm()” and after that he continues to create worms.
I know this has to do with empty indeces in the Array, but the .splice(0) should reset it right?
Help, I’m stuck for hours!