Looking for a way to stop ALL movieclips - actionscript-3

I have a project with a LOT of symbols each playing at certain times.
I would like a command to stop all symbols from playing without having to actually add mc.stop(); for every single one.
I have tried the generic stop(); but that doesnt work
Anybody know anyway to easily do this?

you have to search all of existing child's of your container, then check if that's Movieclip stop its time-line
for example call movieClipStopAll(this) with this following function
function movieClipStopChilds(container:DisplayObjectContainer):void {
for (var i:uint = 0; i < container.numChildren; i++)
if (container.getChildAt(i) is MovieClip)
(container.getChildAt(i) as MovieClip).stop();
}
Edit:
following function also stop inner child movieclips
function movieClipStopAll(container:DisplayObjectContainer):void {
for (var i:uint = 0; i < container.numChildren; i++)
if (container.getChildAt(i) is MovieClip) {
(container.getChildAt(i) as MovieClip).stop();
movieClipStopAll(container.getChildAt(i));
}
}

If you are using Flash Player 11.8 / AIR 3.8 or later, you can just use the built in stopAllMovieClips method.
Recursively stops the timeline execution of all MovieClips rooted at this object.
Usage:
commonParent.stopAllMovieClips();
Where commonParent is the top most object that contains all the MovieClip's you would like to stop. This could be the mainTimeline or stage if you truly wanted to stop everything.
If you only want to stop the immediate children of a parent, use the solution in the first part of payam sbr's answer.

Related

Detect which movieclip you hit (AS3)

Im developing a game in AS3, And I ran into problem.
I have the movieclip: ExitPoints. And inside this movieclip, there some other movieclips like: e_1, e_2, e_3 and e_4.
When the player hit the ExitPoints movieclip, I want the game to trace which movieclip the player is standing on (e_1 or e_2 or e_3 or e_4).
I know I can make a code like:
if(player.hitTestObject(ExitPoint.e_1){
}
but I want to do it automaticlly with creating a lot of if statements.
Any help?
This can be accomplished using a loop that loops through all the exits contained in an array.
var exits:Array = [ExitPoint.e_1, ExitPoint.e_2, ExitPoint.e_3, ExitPoint.e_4];
for(var i:int = 0; i < exits.length; i++) {
if(player.hitTestObject(exits[i])) {
//do something
}
}

Animations won't start from the beginning when calling gotoAndPlay -- Actionscript 3

I have animations in my second frame ( a falling bird and moving pipes ), once i jump to the third frame i give the user the choice to restart the game by doing gotoAndPlay(1) on a button click, the problem is once i go back the the second frame, the animations continue on playing and not starting from the beginning, i want to restart all the frame animations from their initial positions, what i did is the give every object on the stage the starting (X,Y) coordinates in the script, but it gets difficult when i have so many objects, is there a better way of using this!
First things first: if you're making a game, you probably shouldn't be performing any mission-critical logic on the timeline itself. Look into OOP tutorials.
That being said, what you're looking for is a recursive gotoAndPlay, meaning that you want all the children of the stage and all of their children (etc) to play from the first frame:
function recursiveGotoAndPlay(clip:DisplayObjectContainer, frame:int):void
{
for(var i:int = 0; i < clip.numChildren; i++)
{
var child:DisplayObject = clip.getChildAt(i);
if(child is DisplayObjectContainer)
{
recursiveGotoAndPlay(child as DisplayObjectContainer, frame);
}
}
if(clip is MovieClip)
{
MovieClip(clip).gotoAndPlay(frame);
}
}
Instead of calling the native gotoAndPlay(1), you will call recursiveGotoAndPlay(stage, 1).

How to construct nested eventlisteners in AS3

Working a bit with AS3 and hit a wall on how to program through this situation. I have a class which represents a number say 103. I have a movieclip for each digit which I add to a holding movieclip and then add to the stage. I want to enable the ability to single click a digit like the zero in the number 103 and have it react since it is an individual movieclip and at the same time double click the entire number and have that react. Is there a way to cleanly do this wtihout confusing the code below is what I have thus far.
public function test()
{
numberimage = new MovieClip();
var images:Vector.<MovieClip> = generateNumericArray("");
for (var i:int = 0; i < String(value).length; i++) {
var temp:MovieClip = parsevalue(String(value).substr(i,1),images);
temp.x = i*50;
temp.addEventListener(MouseEvent.CLICK,click)
numberimage.addChild(temp);
}
numberimage.addEventListener(MouseEvent.MOUSE_DOWN,drag);
numberimage.addEventListener(MouseEvent.MOUSE_UP,drop);
numberimage.addEventListener(MouseEvent.MOUSE_OVER,doubleClick);
stage.addChild(numberimage);
}
any help on this would be much appreciated
Do, doubleclick on clip : stop listening to main movie, and listen now to sub clips. On click outside the main clip, listen back to the main movie and stop listening to its children.
Not sure if this is 100% what you are looking for: use the following, depending on what you are adding the listener to. You do have to enable double clicking first
numberimage.doubleClickEnabled = true;
numberimage.addEventListener(MouseEvent.DOUBLE_CLICK, click);

As3 - Target all MovieClips?

I was wondering if there is any way to target all MovieClips, so I can for example add effects to all of them.. Would
for each(Mc:MovieClip in stage){
//do sth
}
work?
I wish it was as simple as your suggestion, it would certainly make more sense. Unfortunately, you probably need to do something more like the following:
for(var i:int = 0; i < numChildren; i++)
{
var m:DisplayObject = getChildAt(i);
if(m is MovieClip)
{
// Do some stuff
}
}
This will loop through all the children then check if they are a movieclip, then you can do your stuff.
Alternately you could track every movieclip as it's added to the stage in an array, then reference from that. But that possibly just overkill. Depends on your use case.

how to manipulate movieclip with nested movieclip inside?

i want to fasten the movie clip playing speed twice time, here's the code
mc.addEventListener(Event.ENTER_FRAME,function(e:Event):void{
mc.nextFrame();
});
this works if the movie clip only had one level.but with a nested movieclip, it can't help.
when call mc.stop(); the nested movie clip won't stop.i dispatch various event,like enter frame,EXIT FRAME,RENDER to their parent hope make them move to the next frame,but nested movie clip just won't move.
thought there's one way left to get all movieclip object under that movieclip to make them move,but that's not a good choice, since i cant predict what code inside there.
You could use a function like this:
function callOnChildren(container:DisplayObjectContainer, method:String, args:Array = null):void
{
for(var i:int = 0; i < container.numChildren; i++)
{
var child:DisplayObject = container.getChildAt(i);
child[method].apply(child, args);
}
}
And then call nextFrame() on all children like so:
callOnChildren(mc, "nextFrame");
Other methods work too:
callOnChildren(mc, "gotoAndStop", [3]);