Detect which movieclip you hit (AS3) - actionscript-3

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

Related

How to access all movieclips (and movieclips inside a movieclips,...) at a sametime with as3?

I am using Adobe Animate (or Adobe Flash Professional) and I often navigate timeline with as3.
I want to reset all movieclips (and movieclips inside a moviclip) when the stage reach to an exact frame.
like:
if (this.currentFrame == 120)
{
allMovieClips.gotoAndPlay(1);
}
I am thinking about taking access to all movieclips in library but I don't know how.
Is there any way to do that?
You cannot access things in Library as the Library is a design-time concept. If you want to reset all the MovieClip instances presently attached to the Stage, you do the following:
import flash.display.Sprite;
import flash.display.MovieClip;
// Start resetting them from the topmost timeline.
reset(root as Sprite);
function reset(target:Sprite):void
{
// First, browse all the children of the target.
for (var i:int = 0; i < target.numChildren; i++)
{
var aChild:Sprite = target.getChildAt(i) as Sprite;
// If a child is a container then go recursive on it.
if (aChild) reset(aChild);
}
// Second, if the target is not only the container
// of other things but a MovieClip itself then rewind it.
if (target is MovieClip)
(target as MovieClip).gotoAndPlay(1);
}

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.

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();
}
} }

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.