Highlighted

How to Remove all Children from a Specified Array?

Explorer ,
Sep 28, 2019

Copy link to clipboard

Copied

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.

TOPICS
ActionScript

Views

452

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to Remove all Children from a Specified Array?

Explorer ,
Sep 28, 2019

Copy link to clipboard

Copied

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.

TOPICS
ActionScript

Views

453

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 28, 2019 0
Adobe Community Professional ,
Sep 29, 2019

Copy link to clipboard

Copied

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);
}

 

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2019 0
Explorer ,
Sep 29, 2019

Copy link to clipboard

Copied

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()

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2019 0
Adobe Community Professional ,
Sep 29, 2019

Copy link to clipboard

Copied

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);
        }
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2019 0
Explorer ,
Sep 29, 2019

Copy link to clipboard

Copied

Thank you, that worked. I only have one more issue, and I have absolutely no idea why it's occurring, but when the game is over, and you go back to the menu and hit play again, everything works as it should, except for the weapons. In this game, the characters you play as as well as your enemies have random weapons they hold, and when they die, they drop them, and you can pick them up, throw them etc. For some reason, the behavior of these weapons becomes extremely odd after playing one round. You won't be able to drop or pick any up, up until enough enemies show up and drop a certain number of weapons themselves. It's almost as if the "ground_weapons" array doesn't exist, until some random point that I don't know.

 

I use this to clear the array when the game is over: 

ground_weapons.length = 0;

 And here is part of the code for one of the characters you play as for dropping/picking up weapons:

for (var i:Number = 0; i < ground_weapons.length; i++)
		{
			if (sanford_weaponSwitch_key == "pressed")
			{
				if (sanford.pickUp_hitbox.hitTestObject(ground_weapons[i]))
				{
					if (sanford_pickUpReady == "true")
					{
						if (sanford_drop=="false")
						{
							sanford_dropReady = "false";
							sanford_punching = "false";
							sanford_function_drop();
							sanford_pickingUp = "true";
							sanford_pickUp = "true";
							sanford_pickUpReady = "false";
							sanford_dropReady = "false";
						}
					}
				}
				else
				{
					//Drop
					if (sanford_weaponType!="unarmed")
					{
						if (sanford_dropReady=="true")
						{
							sanford_function_drop();
							sound_grab();
							sanford.hand.hand.gotoAndPlay("open");
							sanford_pickUpReady = "false";
							sanford_dropReady = "false";
							sanford_weapon = "fists";
							sanford_weaponType = "unarmed";
						}
					}
				}
			}
		}

I know a lot of this is out of context, but basically, I'm trying to get "sanford_function_drop" to happen, which is its own function that makes the character pick up the weapon. When I use trace, this doesn't seem to occur at all during the second round of the game. Is there something I need to clear or do to the array?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2019 0
Adobe Community Professional ,
Sep 29, 2019

Copy link to clipboard

Copied

If one of your other functions changes the contents of ground_weapons, then this for loop won't work correctly. Doing :

 

for (var i:int = ground_weapons.length-1; i >=0; i--)

 

would solve that.

 

I hadn't thought about setting the length of an array to zero as a way to clear it. I would usually say: ground_weapons = [];

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2019 0
Explorer ,
Sep 29, 2019

Copy link to clipboard

Copied

Thank you, but I tried that look and I'm still getting the same problem. 😞

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2019 0
Explorer ,
Sep 29, 2019

Copy link to clipboard

Copied

Ah hang on, I fixed it. Long story short, because of the way I have coded everything, if I don't add a test MC to the array at the start of the game, the drop/pick up behavior is allover the place, inconsistent and glitchy. It was the one item I forgot to remove and splice from the array when the game was over. So I guess when it tried to re-add it when the game restarted, something weird happened with the array. But thanks anyway, you've been a life saver!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2019 0