Skip to main content
Inspiring
March 22, 2009
Answered

removeChildAt does not work

  • March 22, 2009
  • 2 replies
  • 468 views
Hi,

I have a simple preloader that loads another swf which consist of one image on first frame timeline.
Swf loads but text that shows loading progress stays visible although I call removeChildAt(0) on Event.COMPLETE.
Text field also does not refresh - all numbers shown on top of each other.
When I trace numChildren it traces 7???
Could somebody take a look?
This topic has been closed for replies.
Correct answer Rothrock
The function loop is called each time there is some progress in the loading of your loader. At the end of each loop you addChild. So my guess is that the function gets called 7 times and that you have added the percent text field 7 times. So you will either need to removeChild 7 times (or as many times as it was added) or you might want to restructure your code so that percent is only created once and added to the diplay list once.

Remember code generally does what we tell it, not what we want!

2 replies

Inspiring
March 22, 2009
Well that is an awful lot of code for me to look at. So take your own stab at it using the same tricks as before.

Add strategic trace statements to be sure that "this" is what you think it is. And if need be put a trace before each addChild and removeChild to see how many you are getting.

Also I haven't done a lot of this, but I think you generally want to removechild from higher numbers to lower numbers. Because if there are 4 children and you remove them in the order 0,1,2,3 it won't work, because as soon as you remove zero there is no 3 anymore. So instead, if you know there are 4 children and you want the all gone, remove them in the order 3,2,1,0.

I think.

Post back if you still can't figure it out.
igreckAuthor
Inspiring
March 22, 2009
Figure it out.
Didn't have anything to do with removechildAt() method.
I have tween upon completion of which addEventListeners(); was called. I was clicking buttons before tween was complete.
RothrockCorrect answer
Inspiring
March 22, 2009
The function loop is called each time there is some progress in the loading of your loader. At the end of each loop you addChild. So my guess is that the function gets called 7 times and that you have added the percent text field 7 times. So you will either need to removeChild 7 times (or as many times as it was added) or you might want to restructure your code so that percent is only created once and added to the diplay list once.

Remember code generally does what we tell it, not what we want!
igreckAuthor
Inspiring
March 22, 2009
Thank you Rothrock, that did the trick.
However I have another class that loads images on the click of buttons. This class uses same removeChildAt(0) method to remove previous image. It will remove image if I click on the same button a few times. It is probably similar problem but I can't see where it lies.
Please take a look.