标签:des style blog color io for ar art div
package game.mananger { import flash.events.TimerEvent; import flash.utils.Dictionary; import flash.utils.Timer; /** *提供 一个 1秒间隔不断跑的timer,可以注册几秒钟回调, * 用于提高性能,全局仅有这一个timer * @author Administrator * */ public class GTimerManager extends BaseManager { private var _timer:Timer; private var _funAry:Array; public function GTimerManager() { if(!_timer) { _timer = new Timer(1000); _timer.addEventListener(TimerEvent.TIMER,_interval); _timer.start(); _funAry = []; } } private function _interval(evt:TimerEvent):void { var len:int = _funAry.length; for(var i:int = 0;i<len;i++) { var info:GFunInfo = _funAry[i]; if(info.isReset) { info.temp ++; if(info.temp % info.delay == 0){ if(info.fun!=null) { info.fun(); } } }else if(_timer.currentCount % info.delay == 0) { if(info.fun!=null) { info.fun(); } } } } /** *注册函数进去 * @param fun * @param senconds 几秒间隔 */ public function add(struct:GFunInfo):void { if(_funAry.indexOf(struct)==-1) { _funAry.push(struct); if(struct.isReset){ struct.temp = 0; } } } /** *移除函数 * @param fun * */ public function remove(struct:GFunInfo):void { var index:int = _funAry.indexOf(struct); if(index != -1) { _funAry.splice(struct,1); } } /** *销毁 * */ public function destory():void { _funAry.length = 0; _timer.stop(); _timer = null; } } }
package game.mananger { public class GFunInfo { /** * * @param fun 回调函数 * @param delay 间隔秒数 * @isReset isReset 重新add后是否继续上次的计数 */ public function GFunInfo(fun:Function,delay:int,isReset:Boolean) { this.fun = fun; this.delay = delay; this.isReset = isReset; } public var fun:Function; public var delay:int; public var isReset:Boolean; public var temp:int; } }
testGtimer(); private function testGtimer():void { var g:GTimerManager = new GTimerManager(); g.add(new GFunInfo(testGfun,5,true)); g.add(new GFunInfo(testGfun2,1,false)); } private function testGfun():void { trace("testGfun"); } private function testGfun2():void { trace("testGfun2"); }
标签:des style blog color io for ar art div
原文地址:http://www.cnblogs.com/as3lib/p/3930641.html