Skip to main content
Inspiring
September 29, 2019
Question

How to Remove all Children from a Specified Array?

  • September 29, 2019
  • 1 reply
  • 920 views

I've looked this up multiple times and every method I've tried returned an error. I'd like to know how to remove all MC's added to a specific array. I made this game where enemies drop different kinds of weapons when defeated, and those weapons are added to the array "ground_weapons". When the game is over, is there a way to effeciently remove all of those children added to that array, and if so, what is it? Thank you.

This topic has been closed for replies.

1 reply

Colin Holgate
Inspiring
September 29, 2019

If all of the MCs had the same parent, and if you were using HTML5 Canvas, there is a removeAllChildren() method. If it's AS3, or the MCs have different parents, this would work:

 

var mc:MovieClip;
while(ground_weapons.length>0){
	mc = ground_weapons.shift();
	if(mc.parent) MovieClip(mc.parent).removeChild(mc);
}

 

 

You could cheat a little and skip the checks for if the movieclip currently has a parent, or what that parent is, by adding it to the current level then removing it:

var mc:MovieClip;
while(ground_weapons.length>0){
	mc = ground_weapons.shift();
	addChild(mc);
	removeChild(mc);
}

 

 

 

 

Inspiring
September 29, 2019

It is the AS3 canvas, and with both of those codes I get the errors:

 

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at MethodInfo-1594()
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-1518()

Colin Holgate
Inspiring
September 29, 2019

It's possible that some other code already got rid of the movieclip, but it was still listed in the array. This might solve that:

 

var mc:MovieClip;
while(ground_weapons.length>0){
	mc = ground_weapons.shift();
        if(mc){
	     if(mc.parent) MovieClip(mc.parent).removeChild(mc);
        }
}