Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Flash(AS3) Unable to Remove All Children


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 glu   Members   -  Reputation: 115

Like
0Likes
Like

Posted 19 February 2013 - 06:42 PM

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.



Sponsor:

#2 Jokermagician   Members   -  Reputation: 128

Like
0Likes
Like

Posted 20 February 2013 - 03:21 AM

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



#3 BloodyEpi   Members   -  Reputation: 688

Like
0Likes
Like

Posted 20 February 2013 - 07:25 AM

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, 20 February 2013 - 07:32 AM.


#4 stupid_programmer   Members   -  Reputation: 1234

Like
0Likes
Like

Posted 20 February 2013 - 10:43 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS