Making Functions For Movieclips - actionscript-3

I'm trying to create a function that modifies all of my movieclips.
This is what I have tried, but it's not working:
for (var i:Number = 0; i<50;i++) {
checkLine(this["line" + i + "_mc"]);
}
my movieclips are all on the stage and have instance names of line0_mc, line1_mc, up until line_49mc. What have I done wrong?

I think the best solution is to put each MovieClip in an array for better accessibility.

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
}
}

Looking for a way to stop ALL movieclips

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.

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]);

Arrange z order of two movieclips on click

i have 2 layes with one movieclip each.
How is it possible to bring forward the clickable movie clip and the other goes backwards?
I tried some methods but i didn't had any luck.
If it is not too much code and someone has a little time please help me to solve it.
Thank you all again!
put both movie clip in arraylist suppose we say (zorder),and call bringttofronthandler or sendtobackhandler on some event as you wish.
private function recalculateDepth():void
{
for (var i:int = 0; i < zorder.length; i++)
{
zorder.getItemAt(i).depth = i;
}
}
protected function bringToFrontHandler(event:Event):void
{
zorder.removeItem(selectedItem);
//set to top of array
zorder.addItem(selectedItem);
recalculateDepth();
}
protected function sendToBackHandler(event:Event):void
{
zorder.removeItem(selectedItem);
//set to bottom of array
zorder.addItemAt(selectedItem, 0);
recalculateDepth();
}
}
You should check these links to understand the concept of display lists.
http://www.adobe.com/devnet/flash/quickstart/display_list_programming_as3.edu.html
http://www.republicofcode.com/tutorials/flash/as3displaylist
http://active.tutsplus.com/tutorials/actionscript/as3-101the-display-list/
All these will help you understand the operations that you can perform on various objects on screen including the swap functionality that you are asking for.
swapChildren() or swapChildrenAt();
this.swapChildren(mc1,mc2)
or
this.swapChildrenAt(0,1)