how to manipulate movieclip with nested movieclip inside? - actionscript-3

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

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 - 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])){
}
}

Stop(); in nested movie clip doesn't work AS3

I have a movie clip on my timeline, which I then move on main timeline using classic tween from one side to another. I do not want that mc to loop so once the animation inside it finishes it should stop and all frames should be visible until the tween on main timeline finishes. To stop it from looping I added new keframe with stop(); at the end inside the movie clip. But it doesn't work, the movie clip keeps looping. I have changed the property type on first frame in the main timeline from movieclip to Graphic so that I can preview the movie clip in the timeline. In previous versions of Flash it worked always fine, but in CC the stop(); is ignored.
I know AS won't work with Graphics, but as far as the AS is inside that Graphic that shouldn't matter. Could anyone explain it to me and provide some solution please?
Your animation is happening on the root timeline so you need to put your stop(); on the last keyframe on your main time line. Or create the animation inside your movieclip and put the stop(); on the last keyframe in there instead and put the movieclip on the stage.
Try this code it works 100%
Paste the below code your main time line and called MovieClip_name.stopAllClips();
MovieClip.prototype.stopAllClips = function():void {
var mc:MovieClip = this;
var n:int = mc.numChildren;
mc.stop();
for (var i:int=0; i<n; i++) {
var clip:MovieClip = mc.getChildAt(i) as MovieClip;
if (clip) {
clip.stop();
clip.stopAllClips();
}
} }

Clicking on a movieclip inside another movieclip on AS3

Alright, what i need it's simple but its driving me crazy, i want to know if AS3 detects my mouse inside a movieclip.
For example, i have a movieclip instanced "BEframes" which is inside movieclip "BE1" and i want to put him inside a new movieclip instanced "roll". So the order would be roll > BE1 > BEframes.
I want to know if flash will only detect "roll" or he will detect all movieclips, thank you,
for(i=1;i<=77;i++){
var str:String =("BE" + i);
this[str].BEframes.gotoAndStop(i);
this[str].addEventListener(MouseEvent.CLICK, clique);
this[str].addEventListener(MouseEvent.ROLL_OVER, over);
this[str].addEventListener(MouseEvent.ROLL_OUT, out);
}
function clique(evt:MouseEvent):void{
var botao:String = evt.currentTarget.name.toString();
var num:String = botao.replace("BE", "");
parede_esquerda.gotoAndStop(num);
}
function out(evt:MouseEvent):void {
evt.currentTarget.gotoAndPlay("out");
}`enter code here`
function over(evt:MouseEvent):void {
evt.currentTarget.gotoAndPlay("over");
}
*
Probably, you should use MOUSE_OVER and MOUSE_OUT instead of ROLL_OVER and ROLL_OUT.
this[str].addEventListener(MouseEvent.MOUSE_OVER, over);
this[str].addEventListener(MouseEvent.MOUSE_OUT, out);
To avoid receiving mouseEvent for movieClips set mouseEnabled to false, i.e if you don't want clip roll's mouse event setroll.mouseEnabled = false so that the object below will receive mouseEvent