[AS3] Removing childs of the same name...

Discussion in 'Programming' started by Miquexia, Mar 27, 2011.

  1. #1
    Hello there,

    I'm working on a game that lets objects of the same name spawn everytime the timer triggers (Which is every 5 seconds). The objects that spawn on the stage get pushed into an array, so I can let them each fall down by using the ENTER_FRAME function. Here's a piece of the code:

    
    timer = new Timer(5000, 6);
    timer.start();
    
    timer.addEventListener(TimerEvent.TIMER, onTime);
    
    private function onTime(event:TimerEvent):void
    {
    	tekenAntwoord(); // This is the function that adds a new object to the stage called "antwoorden", the same function also pushes that new object intro an array.
    }
    
    private function tekenAntwoord():void
    {
    	antwoorden = new Antwoorden();
    	addChild(antwoorden);
    
    	antwoordenRij.push(antwoorden);
    }
    
    private function onEnterFrame(event:Event):void
    {
    	// It counts the length of the array and than manipulates the objects each. It lets them fall down.
    	for(var tellerAntwoorden:int = 0; tellerAntwoorden < antwoordenRij.length; tellerAntwoorden ++) {
    		antwoordenRij[tellerAntwoorden].y += 3;
    	}
    
    	// A hitTest.
    	if (avatar.hitTestObject(antwoordenRij[tellerAntwoorden])) {
    		// Remove the objects, pretty please? :(
    	}
    }
    
    Code (markup):
    Now, the objects that fall down can be cought by another object called avatar. So to do this I have added a hitTest as you can see in the code above. Now the thing I'd like to do is removing ALL the objects that fall down. (The one called antwoorden). I've tried multiple things like:

    
    while(antwoorden.numChildren)
    {
        antwoorden.removeChildAt(0);
    }
    
    Code (markup):
    and

    
    for(var i:int = 0; i < antwoorden.numChildren; i ++)
    {
        removeChild(antwoorden);
    }
    
    Code (markup):
    Etc. But all it does is removing one object from the stage.

    Has this something to do with the timer i'm using? Or because I'm using an array? I have NO idea and it's driving me nuts :(

    Can anyone help this poor guy out?

    Much appreciated.

    Ozjifu
     
    Miquexia, Mar 27, 2011 IP