flash Timer 性能优化,每几秒间隔一次

timer.stop后timer.currentCount没有重置,timer.reset后,currentCount重置了。
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");
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值