Hi, greetings from Portugal! My first post here... :D
I can't believe you posted this!! I've been struggling with this on the other day... And now I found the answer!
Congratulations on your site and this forum!
This is a discussion on Tip and Trick - DisplayObjectContainer.removeChilAt within the Tutorials forums, part of the Flash English category; After having seen the methods target and currentTarget , here we are with a new tip of the day. A ...
After having seen the methods target and currentTarget , here we are with a new tip of the day.
A new thing, I discovered while developing an application:
Actionscript 3.0 does not have a method which allows to remove all the MovieClips placed into another MovieClip.
To do it, we need a for cycle (or while if you prefer) using the property numChildren.
Let us see how to do it" Each time that we remove a MovieClip, if the depth of that MovieClip is the highest depth used or if that MovieClip is the only one present, the problem is not encountered and the MovieClip is completely removed.
Instead, if we have an undefined number of MovieClips inside an another MovieClip as container and we remove the nested MovieClip with the highest depth, the next one will take its place.
Let us take a look at a concrete example.
The following example is the INCORRECT way:
I create a FLA, into which I create in runtime a MovieClip container and using a for cycle, create a random number of nested MoveClips of rounded shape.
using the action of a button, I would now like to remove all the MovieClips placed into the MovieClip container:Code:var n:Number=Math.round(Math.random()*50); var container_mc:MovieClip=new MovieClip(); addChild(container_mc); for(var i:int=0;i < n;i++) { var clip:MovieClip=new MovieClip(); var xx:Number=Math.random()*stage.stageWidth; var yy:Number=Math.random()*stage.stageHeight; var radius:Number=Math.random()*40; clip.graphics.beginFill(0xFF0066,1); clip.graphics.drawCircle(xx,yy,radius); container_mc.addChild(clip); }
as we can see, we should need a simple for cycle with an crescent iteration and of maximum value the number of nested MovieClips inside of container_mc retrieved from the property numChildren. Keeping in mind what I wrote first, if we remove a MovieClip the next one takes its depth, so the cycle is incorrect for the following reasons:Code:rimuovi_btn.addEventListener(MouseEvent.CLICK,removeClips); function removeClips(event:MouseEvent):void { for(var i:int=0;i < container_mc.numChildren;i++) { container_mc.removeChildAt(i); } }
When the cycle starts, the variable "i" is equal to zero. The MovieClip in depth 0 is removed and the MovieClip which was in depth 1, passes to depth 0. The same process continues as the cycle goes until the maximum iteration has been reached. In this case, the for cycle is useless as its iteration continues to increase while the nested MovieClips pass to the depth 0 one after the other. In fact, when the button is pressed on the below SWF, not all the MovieClip are removed.
So, the cycle needed to correctly remove all the MovieClips with a single click of a button is the following:
We impost as an initial value for i: numChildren-1. Flash sees the nested Moviclip as an array and for that reason, we can not use the array"s length itself as at that point it does not exist yet.Code:rimuovi_btn.addEventListener(MouseEvent.CLICK,removeClips); function removeClips(event:MouseEvent):void { for(var i:int=container_mc.numChildren-1;i >= 0;i--) { container_mc.removeChildAt(i); } }
As a maximum iteration, we use zero and decrease the value of "i".
In the following SWF is the result. We can now see that all the MovieClip are being removed as wanted.
or:
Stay tuned !Code:rimuovi_btn.addEventListener(MouseEvent.CLICK,removeClips); function removeClips(event:MouseEvent):void { for(var i:int=container_mc.numChildren-1;i >= 0;i--) { container_mc.removeChildAt(0); } }
Last edited by Flep; 28-08-08 at 05:43.
Hi, greetings from Portugal! My first post here... :D
I can't believe you posted this!! I've been struggling with this on the other day... And now I found the answer!
Congratulations on your site and this forum!
Hi Ifdesign and welcome on board :)
Nice to know it helps you ;)
Flep .... Here again!
Through this tutorial, I am trying to remove a container called FLVplayer .... I am not sure if I am doing wright because gives me the follows error
1120: Access of undefined property FLVPlayer.
It means Flash does not find any object with the name FLVPlayer.
I sort it ... there is a miss-spelling .... but it does not delete the movie (flvPlayback).
==========
The other way I tried it was :
public function _deciClick(event:Event):void {
//deciMC.addEventListener(MouseEvent.CLICK, removeFLV);
removeChild(fppRecalling);
fppDecision=new FLVPlaybackPro();
fppDecision.x = 120;
fppDecision.y = 100;
fppDecision.visible=true;
fppDecision.autoPlay=false;
fppDecision.source = "100.flv";
fppDecision.skin = "SkinUnderPlayStopSeekMuteVol.swf";
fppDecision.skinBackgroundColor=0x666666;
FLVPlayer.addChildAt(fppDecision,0);
var shadow:DropShadowFilter=new DropShadowFilter;
shadow.distance=5;
shadow.angle=30;
shadow.alpha=0.2;
shadow.color=0x000000;
FLVPlayer.filters=[shadow];
//fppDecision.addEventListener(VideoEvent.READY, deciHandler);
}
it gaves me the following error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at holdingPage/_deciClick()
Any suggestion?!
Many thanks in advance....
Here again .... following your tutorial... I try to remove the FLVplayback with the following code:
public function removeFLV(event:MouseEvent):void {
for (var i:int=0;i < FLVPlayer.numChildren;i++) {
FLVPlayer.removeChildAt(0);
event.updateAfterEvent();
}
}
the FLVPlayback is removed from the displayList but I can keep listen the movie. So, I guess the movie has not been removed from the stage.
... what I am doing wrong?
thanks!
Keep with the same issue! Please any help it would be greatfull!
If I remove the childs with the follow code:
public function removeFLV(event:MouseEvent):void {
for (var i:int=0; i < FLVPlayer.numChildren; i++) {
FLVPlayer.removeChildAt(0);
event.updateAfterEvent();
}
}
I need to stop the flvskin manually in order to stop the movie playing. I wonder how I can stop downloading the movie to the swf movie.
I have been trying pause() and stop() to the different movies but it doesn't work.
Flep do you know any tutorial which could help with this matter. I would appreciate a lot.
I am becomingand obsessive ...
again, again ... many thanks!
Hi
Are you trying to remove the component from the stage ?
If yes, why you say: FLVPlayer.removeChildAt(0); ?!?
You must say:
removeChild(FLVPlayer);
Because if I do that it gives me the following error
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at holdingPage/removeFLV()
if I do FLVPlayback.removeChildAt(0);
I remove the child but I do have to stop manually the movie otherwise I listen the movie in the background
![]()
Bookmarks