Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 removing all children from array in a class from parent class

In the Level 1 class (Parent) I generate citizens as seperate objects (c) to walk from left or right across the stage. all of these get added to an array called citizens:

if (citizens.length < 10)
        {
            // create citizen
            var c:Citizen = new Citizen(side,speed,yPos);
            addChildAt(c, 2);
            citizens.push(c);
        }

I want to remove each instance of the class and also remove the event listener that is attached to it in the class:

this.addEventListener(Event.ENTER_FRAME,moveCitizen);

Would I use a for each then splice from the array? E.G

for each (c in citizens) {
removeEventListener(Event.ENTER_FRAME,moveCitizen);
splice();
}

1 Answers

You could do something similar to the following:

// Creation
if (citizens.length < 10) {        
    // create citizen
    var c:Citizen = new Citizen( side, speed, yPos );
    addChildAt( c, 2 );
    citizens.push( c );
}

// Removal 
for( var i:int = 0; i < citizens.length; i++ ) {
    var c:Citizen = citizens[ i ].pop();
    removeChild( c )
    c.cleanUp();
}

// In Class Citizen
public function cleanUp():void {
    removeEventListener( Event.ENTER_FRAME, moveCitizen );
}
like image 194
Chris Avatar answered Nov 28 '25 17:11

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!