Can't remove MovieClip Child Dynamically

Hi EveryBody,

Please help me out in solving this. I have been trying to remove all CHilds in a Movie CLip since Yesterday without success. Movie Clip is being filled dynamically and i also need to remove its childrens dynamically.

tempSub is a MovieCLip created inside code;

**function removeSub(){

for(var i=0; i<tempSub.numChildren; i++){
			
	tempSub.removeChildAt(i);
					}
					
	
trace("CURRENT : TEMP SUB STATUS")

for(i =0; i<tempSub.numChildren; i++)
{
trace("NUM CHILDREN ARE" + tempSub.numChildren);
trace("CHILD " + i + " is" + tempSub.getChildAt(i));

}

}**

So whenever this function is called after the first FOR loop it still shows some value for the numchildren and its child also.

Please tell me why is it not deleting.

Thank You Very Much.

Well the first problem you have is that numChildren will change as the for loop is executing because every time you do removeChild the numChildren value updates. Also, when you remove the child at location 0, the child at location 1 will now go to location 0, so you won’t end up removing all of them. Instead I would just use a while loop:


while(clip.numChildren>0){
  clip.removeChildAt(0)
}

But I don’t see why you can’t remove tempSub from the stage, because that would remove all its children too.

Hey Thanks very much Fidodo…Excellent.
It didn’t come in my mind. It worked.

Actually I tried to remove tempSub by
this.removeChild(tempSub);
or just by removeChild(tempSub)

but it gives me an error

“The supplied DisplayObject must be a child of the caller.”

It confused me as when i addChild in some function X then is it the child of the function or the layer/MovieClip in which it was created. It should be of the MovieClip/layer logically but after getting the above error…

Currently i am trying to add this tempSUb clip in function X and deleting it by calling function removeSub(). But removeChild is not working.

I really Thank you very very much for your quick help.:slight_smile:

Maybe the scope of the tempSub variable isn’t accessible from where you call removeChild. Where do you declare it and how?

It is accessible i think. The flow is lik that.

**1. One MC on stage (name is LR)
2. I go inside and write some code on Layer1 frame1
include somefile.as;

	SOME CODE HERE including 
	var tempSub:MovieClip = new MovieCLip;
	addChild(tempSub);

	//both these above 2 lines are not inside any funcion so i suppose they      are   globol for LR
  1. I addChild(inside one function) in somefile.as and trying to remove(inside second function) it also in that somefile.as
    **
    But there is the error: “The supplied DisplayObject must be a child of the caller.”

Do you think i am doing soemthing wrong.

Now i am into one more problem :frowning:

I have a MovieClip “temp” containing a text and a Button inside it in the Library(through Linkage).

I am adding 5 instances of it currently through my code.

But if i mask it it shows me 5 instances of “temp” with Button inside but the text disappears. If no mask then it shows both…

Please anybody tell what could be the reason.

Sorry for too many questions Guys…it is just some crazy days for me.

You shouldn’t code directly on frames, that might have something to do with the scope getting messed up. I haven’t done any text or masking recently so none of that’s fresh on my mind.

So do you want to say that the best method is to code on the Main Stage itself. NOw i too think that might be a better thing to do.

Hey and i got the text thing. I included the font also in the library and that worked well. It was suggested to me by some kind Guy on some other forum.

Thanks Very much for all your help Mate! Highly appreciated. :slight_smile:

Nope, AS3 has a very awesome new feature. It’s called the document class. Click anywhere empty on the stage and under properties you should see a field for this. Put in the class name of an .as file in the local directory and the flash file will automatically instantiate an instance of the class running whatever code is in the constructor. That’s where you should put all your code. It’s a lot more in line with more robust programming languages and is a very good change.

The only thing I would ever consider appropriate to put in a frame is very simple scripting like gotoAndPlay and stop. I would never recommend programming anything directly on a movie clip as you should export it for actionscript and have it extend a custom class.

Yes i did use Ducument class for this project but i didn’t put all the code over there. But this seems to be a nice idea to put the code outside exept few cases. I hope that way atleast there won’t be any conflicts of scope parent-child related.

Nice Tip Buddy:bounce: