As3 - Target all MovieClips? - actionscript-3

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.

Related

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 - How to put all instances in one code for hit testing?

I have 2.5D game so I cannot put all collision objects in a movieclip container, because I need to keep them as separate display objects. I have multiple instances in the stage. I wouldn't want to go write all the hitTest code for all the objects:
if (player.hitTestObject(object1)
if (player.hitTestObject(object2)
if (player.hitTestObject(object2)... etc
So I would like to know how to hitTest all these instances in one code. I have them added on the stage with instance names, so they are not variables and not added by using the addChild code.
There are plenty of ways to make it less tedious. I'll show a few:
Make a container. You could make a container movie clip and put all the objects in that. Those objects are still individual objects after that (as per your reason in your question for not wanting to go this route). Then you can iterate over all the children of that movie clip:
var i:int = container.numChildren;
while(i--){
if(player.hitTestObject(container.getChildAt(i) as DisplayObject)){
//hit, do something
}
}
Put all the objects in an array, then iterate over that array:
//when you app starts:
var objectArray:Array = [object1,object2,object3]//etc.
//OR, if you have say object1 - object20, you could do something like this:
//vector is basically the same as an array except every item has to be of the same type (on inherit from it)
var objectArray:Vector.<DisplayObject> = new Vector.<DisplayObject>();
for(var i:int=1;i<=20;i++){
var obj:DisplayObject = this.getChildByName("object" + i) as DisplayObject;
if(obj) objectArray.push(obj);
}
//THEN, later, when you do your hit test:
var i:int = objectArray.length;
while(i--){
if(player.hitTestObject(objectArray[i])){
}
}

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

Making Functions For Movieclips

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.

How to find all objects of a type on the stage?

I'm trying to iterate through all the objects in the stage and I'm not sure how to do it.
It's kind of improvised through my previous experience with C# and javascript.
First I need the correct list/array with all the stages children.
I need to check their type. I have a special custom class which extends Sprite with some additional properties only.
Someone proficient in actionscript 3.0 who can show the proper way to do?
for(var obj:DisplayObject in DisplayObjectContainer) {
if(typeof obj == "Pic") {
The easiest would be to use the "is" operator to accertain the object's class.
An example:
for( var i:int = stage.numChildren - 1; i>=0; i-- ) {
if( stage.getChildAt(i) is Pic ) {
// Do stuff with members of Pic class
I don't think you can get to the children of a DisplayObjectContainer like that. You might need to do this:
for(var i=0;i<container.numChildren;i++)
{
if(container.getChildAt(i) is Pic) doSomething();
}
where container is a DisplayObjectContainer.