zoukankan      html  css  js  c++  java
  • 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");
            }
  • 相关阅读:
    java后台对上传的图片进行压缩
    Reflections框架,类扫描工具
    commons-httpclient和org.apache.httpcomponents的区别
    sql里面插入语句insert后面的values关键字可省略
    Callable接口、Runable接口、Future接口
    Java多线程之Callable接口的实现
    说说Runnable与Callable
    论坛贴吧问题:如何终止运行时间超时的线程
    使用Future停止超时任务
    spring的@Transactional注解详细用法
  • 原文地址:https://www.cnblogs.com/as3lib/p/3930641.html
Copyright © 2011-2022 走看看