Joe Lowery
Joe Lowery

Reputation: 572

Loop movieclip in actionscript 3

I'm trying to get a movieclip to start at a specific frame, play 3 times and then stop. My movieclip is in the keyframe. I've tried a couple of different approaches and cannot limit the loop - it just keeps going.

First, I tried embedding this AS within the movieclip (mc_enlightened_nut_glow) on frame 1:

for (var i:Number=1; i<=3;i++){ 
  this.gotoAndPlay(1)
}
this.stop();

And then I tried removing that AS and putting this in the final frame of the entire movie (frame 132) where I had a stop();

for (var i:Number=1; i<=3;i++){ 
mc_enlightened_nut_glow.gotoAndPlay(1)
}
stop();

In both cases, the clip just keeps playing. What am I doing wrong?

Upvotes: 0

Views: 5089

Answers (4)

Moddl
Moddl

Reputation: 360

var loopCounter:int = 0;

last frame

if(loopCounter < 3){
    gotoandPlay(1);
    loopCounter ++;
 }

Upvotes: 2

Nicolas Siver
Nicolas Siver

Reputation: 2885

You don't need any iterations. You will need code in frist frame, and last one (frame 132), and a variable for storing current loop:

As for code in first frame:

if(this.loopCount == undefined){
    //Initiate our stuff
    //Only 3 loops...
    this.loopCount = 3;
    //Code will be invoked only once
    //Deside here, about your starting frame
    //Start first time from the third frame
    gotoAndPlay(3);
}

Code for last frame:

//We have visited last frame
if(--this.loopCount <= 0){
    stop();
    //No more loops!
}

Upvotes: 1

moosefetcher
moosefetcher

Reputation: 1901

You'll need a variable defined prior to the 'loop' of your MovieClip animation (eg, on frame 1 - var counter:int = 0) that gets incremented at frame 132. At that point, check if the variable is being incremented to a value greater than or equal to 3 and stop, if so. Otherwise gotoAndPlay(2).
This is all assuming you don't want to put your code in an external .as file - which is a far more flexible way of working.

Upvotes: 2

Andrey Popov
Andrey Popov

Reputation: 7510

On first frame:

var currentLoops:uint = 0;
var maxLoops:uint = 10; // desired loops

On second frame:

currentLoops++;
if (currentLoops == maxLoops) {
    stop();
}

On final frame:

gotoAndPlay(2); // the frame with the second code (increasing current loops)

Rough, but should work properly.

Upvotes: 1

Related Questions