Sign in to follow this  
glu

Flash(AS3) Unable to Remove All Children

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Edited by BloodyEpi

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this