Flash(AS3) Unable to Remove All Children

Started by
2 comments, last by stupid_programmer 11 years, 2 months ago

Hi, I'm making a game in Action Script, and I need to change levels. The current method I'm using for this, is removing all children from the first level, and then replacing them with the new children from the next level. It loads the children from the next level, and even appears to remove the ones from the first level, but it doesn't. When I move my player arround(which I shouldn't even be able to do since the player should have been deleted too), I will bump into the walls that I placed, even though the only things visible is from the new level. Here is my code for changing levels.


function changeLevel(id)
{
	trace(this.numChildren); //shows all my objects
	
	while(this.numChildren)
		this.removeChildAt(0);
	
	trace(this.numChildren); //shows 0 children
	
	addChild(new swfLoader().loadSWF("level\\" + id.toString() + ".swf")); //class i made for loading SWF files
	
	trace(this.numChildren); //shows children of new level
}

If no one posts anything, then I'll upload a video showing the problem.

Advertisement

Hey there,

I have an idea about your issue, though I would urge you to take my thought with a grain of salt as I have not really done game development (though I would like to, and though I have worked with C++ and currently Java before), this may be wrong, and I haven't worked in depth with actionscript before. Still, I thought I might try to help you out if I could.

However, I looked at your while loop and had an idea. So, I searched around online for a bit and considered a few options. Here's the one I think might work best:

Your current loop:

while(this.numChildren)
this.removeChildAt(0);

I noticed in your while loop that there is only "(this.numChildren)" instead of more of a conditional such as "(this.numChildren > 0)" or something of that matter. Maybe "(this.numChildren > 0)

this.removeChildAt(0);"

would work? I don't know. If not, sorry about that and I hope you get your answer very soon!

Take care!

--Jokermagician

there is a method which removes all the children at once

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#removeChildren()

haven't checked, but i think it may be faster since it doesn't have to decrease all the indices of the elements in the child vector after deleting the first one at index zero

but i think that your code works, the problem might be that even though you deleted all the children you still have a reference to the old level somewhere else, e.g. collisiontesting

As BloodyEpi already mentioned, while you are removing the graphics for your children from your scene you probably aren't removing the references to all those objects in your collision manager. Objects in ActionScript persist until all references to that object have been removed. If you run your game through the profiler you will probably notice your memory usage keeps going up every level change instead of staying around the same.

This topic is closed to new replies.

Advertisement