Hey guys, just trying things out trying to get practice with this thing. Right now I'm trying to get a simple animation to loop over and over after one button press. I decided to try to achieve this I'm trying to use a while loop. But so far I'm having no luck. Once the button is pressed it just plays the movie once. Heres the code I know i'm doing something wrong. I just don't know what, It's most likely something with the scope of the loop. I Just wouldn't know where else to put the loop.

function clickThis(eventType:MouseEvent):void
{
	var p:Number =0;
	
	while (p < 10)
	{
	gotoAndPlay(1);
	p++; 
	}
}

play1.addEventListener(MouseEvent.CLICK,clickThis);

Thanks guys,
-Soapy

Dani AI

Generated

— the while loop is the root problem. ActionScript 3 runs on a single thread, so a tight while inside a click handler blocks the player. Calling gotoAndPlay repeatedly inside that loop just moves the playhead many times before the runtime gets a chance to render a single frame, which is why the movie looks like it only played once.

Both prior suggestions are useful. 's timeline gotoAndPlay on the last frame is the simplest way to loop forever. 's ENTER_FRAME idea is the right general approach when the loop count must be controlled or multiple UI buttons exist. The pattern is: start the clip on click, add an Event.ENTER_FRAME handler on the clip, detect when currentFrame == totalFrames, increment a counter, then either gotoAndPlay(1) to continue or remove the listener and stop() when the target loop count is reached.

import flash.events.MouseEvent;
import flash.events.Event;

var loops:int = 0;
var maxLoops:int = 10;

startBtn.addEventListener(MouseEvent.CLICK, beginLoop);

function beginLoop(evt:MouseEvent):void {
    loops = 0;
    animMC.gotoAndPlay(1);
    animMC.addEventListener(Event.ENTER_FRAME, onAnimFrame);
}

function onAnimFrame(evt:Event):void {
    var mc:MovieClip = evt.currentTarget as MovieClip;
    if (mc.currentFrame == mc.totalFrames) {
        loops++;
        if (loops < maxLoops) mc.gotoAndPlay(1);
        else {
            mc.removeEventListener(Event.ENTER_FRAME, onAnimFrame);
            mc.stop();
        }
    }
}

Notes: use int for counters, remove the listener when finished, and disable the start control while running to avoid reentrancy. For frame-accurate or non-frame-based timing, consider flash.utils.Timer instead.

Recommended Answers

All 2 Replies

Member Avatar for Member #334542

Loop will not help you to loop over your animation. because its working based on your fps which is set wihin your fla. So what you can do is

i) Assume that you have 100 frames
ii) at the end of the 100th frame add an action as gotoAndPlay(1) in timeline. It will loop over your animation forever.

Putting gotoAndPlay() to a frame won`t be such a good idea if there are a lot of buttons. I`d solve this by adding onEnterFrame event handler and looping it until certain condition is met.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.