All pastes #709609 Raw Edit

frameRateKeeper

public text v1 · immutable
#709609 ·published 2007-09-24 20:19 UTC
rendered paste body
Add the following code to a file called frameRateKeeper.as
Then in your main controller, you can initialize it like this:

var frameTracker = new frameRateKeeper(30,this);
this.addChild(frameTracker);

Where this refers to the main stage, and 30 is the frame rate you want.

package{
	import flash.utils.Timer;
	import flash.display.MovieClip;
	import flash.display.Stage;
	public class frameRateKeeper extends MovieClip{
		var requestedFrameRate = 30;
		var frameTracker = 0;
		var currentFrameRate = 30;
		var stageInstance;
		var secondTimer:Timer = new Timer(1000);
		public function frameRateKeeper(num,sI)
		{
			stageInstance = sI;
			requestedFrameRate = num;
			currentFrameRate = num;
			this.addEventListener("enterFrame",function(){ frameTracker += 1;});
			secondTimer.addEventListener("timer", adjustFrameRate);
			secondTimer.start();
		}
		public function adjustFrameRate(ext)
		{
			var curRate = frameTracker;
			if(curRate > requestedFrameRate)
				{
					currentFrameRate --;
					stage.frameRate = currentFrameRate;
				}
			if((curRate < requestedFrameRate) && (currentFrameRate < 90))
			{
				currentFrameRate += requestedFrameRate - curRate;
				stage.frameRate = currentFrameRate;
			}
			//trace("stage set: " + currentFrameRate);
			trace("actual: " + curRate);
			frameTracker = 0;
			//stageInstance.frameRateCounter.text = stage.frameRate;
		}
	}
}