/* * FrameTimer.as * Copyright: 2008 Touch My Pixel - www.touchmypixel.com - contact@touchmypixel.com * Please see http://blog.touchmypixel.com/archives/14 for discussion */ package com.touchmypixel.utils { import com.adobe.utils.DictionaryUtil; import flash.display.Stage; import flash.events.Event; import flash.events.EventDispatcher; import flash.utils.Dictionary; public class FrameTimer { private static var frameRate:int = 35; private static var scope:Stage; private static var timeouts:Dictionary; private static var intervals:Dictionary; private static var timoutsCounter:int = 1; private static var intervalsCounter:int = 1; private static var millisecondsPerFrame:int = 0; public function FrameTimer() {} public static function init(_scope:Stage) { frameRate = _scope.frameRate; scope = _scope; timeouts = new Dictionary(); intervals = new Dictionary(); millisecondsPerFrame = Math.round(1000 / frameRate); scope.addEventListener(Event.ENTER_FRAME, doTimer); } static private function doTimer(e:Event):void { var to:TimeoutItem; for each(to in timeouts) { if (--to.framesLeft == 0) { to.callback(); delete timeouts[to]; } } var iv:IntervalItem; for each(iv in intervals) { if (--iv.framesLeft == 0) { iv.callback(); iv.framesLeft = iv.frameInterval; } } } /*==================================================================================*/ public static function setFrameTimeout(callback:Function, frames:int):int { if (scope != null) { timeouts[++timoutsCounter] = new TimeoutItem(frames, callback); return(timoutsCounter); }else { trace("Please INIT with scope"); } return(0); } public static function setTimeout(callback:Function, milliseconds:int):int { if (scope != null) { timeouts[++timoutsCounter] = new TimeoutItem(Math.round(milliseconds/millisecondsPerFrame), callback); return(timoutsCounter); }else { trace("Please INIT with scope"); } return(0); } public static function clearTimeout(key:int) { delete timeouts[key]; } /*==================================================================================*/ public static function setFrameInterval(callback:Function, frames:int):int { if (scope != null) { intervals[++intervalsCounter] = new IntervalItem(frames, callback); return(intervalsCounter); }else { trace("Please INIT with scope"); } return(0); } public static function setInterval(callback:Function, milliseconds:int):int { if (scope != null) { intervals[++intervalsCounter] = new IntervalItem(Math.round(milliseconds/millisecondsPerFrame), callback); return(intervalsCounter); }else { trace("Please INIT with scope"); } return(0); } public static function clearInterval(key:int) { delete intervals[key]; } /*==================================================================================*/ public static function clearAll() { timeouts = new Dictionary(); intervals = new Dictionary(); } public static function clearAllTimeout() { timeouts = new Dictionary(); } public static function clearAllIntervals() { intervals = new Dictionary(); } } } internal class TimeoutItem { public var callback:Function; public var framesLeft:int; public function TimeoutItem(_framesLeft:int, _callback:Function) { callback = _callback; framesLeft = _framesLeft; } } internal class IntervalItem { public var callback:Function; public var framesLeft:int; public var frameInterval:int; public function IntervalItem(_frameInterval:int, _callback:Function) { callback = _callback; frameInterval = _frameInterval; framesLeft = _frameInterval; } }