package { import flash.display.Stage; import flash.display.MovieClip; import flash.utils.Timer; import flash.events.TimerEvent; import flash.events.MouseEvent; import flash.events.Event; public class RateController extends MovieClip { static var switchStateTimer:Timer; // Timer used for when to actually switch states after mouse has left stage static var firstRunTimer:Timer; // Timer used to allow full framerate briefly on startup static var active:Boolean = true; // Whether the animation is active static var instantChange:Boolean = true; // Whether we're going to instantly change the frame rate static var firstRun:Boolean = true; // Animate normally for duration of timer at startup var sleepFrameRate:uint; // The frame rate for when the mouse is not on the stage var activeFrameRate:uint; // The frame rate for when the mouse IS on the stage var switchStateDelay:Number; // How long to animate at full rate after the mouse cursor has left the stage var currentStage:Stage; // A Stage that points to our root stage, we need this to set the framerate // Constructor public function RateController(theStage:Stage, theSleepFrameRate:uint, theActiveFrameRate:uint, theSwitchStateDelay:Number) { this.currentStage = theStage; this.sleepFrameRate = theSleepFrameRate; this.activeFrameRate = theActiveFrameRate; this.switchStateDelay = theSwitchStateDelay; // Create (but not start!) our timer to switch sleep/wake after a given delay switchStateTimer = new Timer(this.switchStateDelay); // This sleep listener will always listen for the mouse leaving the stage this.currentStage.addEventListener(Event.MOUSE_LEAVE, switchController); // When we start, we want to run at full speed for a little bit to allow any // initial animation to happen at the correct rate, and then after a small while // put the animation to sleep (if the mouse is moving over it it'll instantly wake // up anyway) if (firstRun == true) { firstRunTimer = new Timer(this.switchStateDelay); firstRunTimer.addEventListener(TimerEvent.TIMER, switchStates); firstRunTimer.start(); } } // End of constructor // Function to control the frame rate switching private function switchController(e:Event):void { // If we're going to sleep, set the sleep frame rate AFTER the timer delay if (instantChange == false) { // If we're going to sleep, switch states after the timer delay switchStateTimer.addEventListener(TimerEvent.TIMER, switchStates); switchStateTimer.start(); } else // If we're waking up, we want to set our active frame rate instantly! { // switchStates expects a timer event, so we have to pass it a bogus one. // Bit of a bodge, I know =/ var foo:TimerEvent; switchStates(foo); } } // End of switchController function // Function to actually switch the states private function switchStates(e:TimerEvent):void { // If we haven't been called from our initial startup timer if (firstRun == false) { // Stop the timer and remove the event listener switchStateTimer.stop(); switchStateTimer.removeEventListener(TimerEvent.TIMER, switchStates); // Swap sleep to wake or vice versa if (active == false) // If the animation's asleep, wake it up! { // Set our flags active = true; instantChange = false; // Put the animation to it's waking frame rate and remove the listener for mouse activity on the stage this.currentStage.removeEventListener(MouseEvent.MOUSE_MOVE, switchController); this.currentStage.frameRate = activeFrameRate; } else // If the animation's awake, put it to sleep! { // Set our flags so we know we're not active, and we want to instantly jump // to our active framerate when the mouse returns to the stage active = false; instantChange = true; // Put the animation to it's sleeping frame rate and add a listener for mouse activity on the stage this.currentStage.frameRate = sleepFrameRate; this.currentStage.addEventListener(MouseEvent.MOUSE_MOVE, switchController); } } else // If we HAVE been called on initial startup (firstRun == true) { // Flip the flags so that: firstRun = false; // We will no longer be in firstRun mode active = false; // Assume mouse not initially over stage, so the animation is in sleep mode instantChange = true; // When we activate, we want the full frame rate instantly // Put the animation to it's sleeping frame rate and add a listener for mouse activity on the stage this.currentStage.frameRate = sleepFrameRate; this.currentStage.addEventListener(MouseEvent.MOUSE_MOVE, switchController); // Remove our firstRun timer firstRunTimer.stop(); firstRunTimer.removeEventListener(TimerEvent.TIMER, switchStates); } } // End of switch states function } // End of RateController class } // End of package