Flash ActionScript (AS3) 3.0 Making a Variable Speed MovieClip

By | February 15, 2012

I needed to make a MovieClip run potentially at different frame rates.  This is pretty easy to do, but I figured others might want example code, so I’m posting it here.  I noticed over at StackOverflow, people were suggesting using the Timer class, but I think an ENTER_FRAME solution is better, especially for trying to make the MovieClip run faster than the frame rate of the real SWF.

For this code, I used Flash CS5 and created a TextInput instance with the name “newrate“. I created a Button with the name changeratebutton, which reset the processing based on the user’s input. The animation is in a MovieClip on the stage, and the instance name is anim.

The idea is very simple: if the clip is set to run at the same rate as the stage, just use play(). If it’s different, compute the number of frames we need to advance (frameIncrement), knowing that we will be given control on each frame based on the SWF’s frame rate. When we get control in a frame, advance the playhead by the whole number of frames we should advance, but accumulate the decimal portion in another value which accumulates (accumulatedPortion). We add that accumulation into the computation for advancing the playhead. Lastly, if we reach the end of the movie, move the playhead back to the start but add in any frames we should skip based on the frame increment and any accumulated frame amount.

Here is the code:

var desiredrate:int = parseInt(newrate.text);
newrate.text = String(stage.frameRate);
var frameIncrement:Number;
var accumulatedPortion:Number;

function changerate (e:Event) {
	stopAnim();
	desiredrate = parseInt(newrate.text);
	startAnim();
}

changeratebutton.addEventListener(MouseEvent.CLICK, changerate);

function startAnim () {
	if (desiredrate == stage.frameRate) {
		anim.play();
	} else {
		accumulatedPortion = 0;
		frameIncrement = desiredrate/stage.frameRate;
		anim.addEventListener(Event.ENTER_FRAME, checkTime);
	}
}

function stopAnim () {
	anim.removeEventListener(Event.ENTER_FRAME, checkTime);
}

function checkTime (e:Event) {
	var framesToAdvance:int;
	var nextFrame:int;

	framesToAdvance = Math.floor(accumulatedPortion + frameIncrement);
	accumulatedPortion = (accumulatedPortion + frameIncrement) - framesToAdvance;

	nextFrame = anim.currentFrame + framesToAdvance;

	if (nextFrame > anim.totalFrames) {
		nextFrame -= anim.totalFrames;
	}

	if (framesToAdvance >= 1) {
		anim.gotoAndStop(nextFrame);
	}
}

anim.stop();
startAnim();

It should be pretty straightforward, but I’m certainly open to clarifying anything people want.

Category: AS3

2 thoughts on “Flash ActionScript (AS3) 3.0 Making a Variable Speed MovieClip

  1. Greg

    This script doesn’t work for me. As soon as I add it to the scene, my textbox and button just flicker all over the window, and my video dissapears. I’m doing a projector export.

    Any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *