About

<#TEMPLATE_INCLUDE_NINEPAGE_ABOUTME#>
  • Jul

    28

    Flash 中有时为了提高性能,采用延迟刷新,一个 callLater 全局函数与 一个 CallLater 类的下载: 延迟刷新.rar

    我比较喜欢这个类:

    package
    {
        import flash.display.Shape;
        import flash.events.Event;
        import flash.utils.Dictionary;

        public class CallLater extends Shape
        {
            protected static var _instance:CallLater;
            protected var callLaterHash:Dictionary;

            public function CallLater()
            {
                callLaterHash = new Dictionary();            
            }

            //p_frameDelay需要被延迟的帧数
            public static function call(p_function:Function, p_frameDelay:uint = 1, p_params:Array = null):void 
            {
                getInstance().call(p_function, p_frameDelay, p_params);
            }


            public function call(p_function:Function, p_frameDelay:uint, p_params:Array):void
            {
                callLaterHash[p_function] = new CallLaterData(p_frameDelay, p_params);

                if (hasEventListener(Event.ENTER_FRAME) == false) { 
                    this.addEventListener(Event.ENTER_FRAME, onFrame);
                }
            }

            protected function onFrame(p_event:Event):void 
            {
                var hasRun:Boolean = false;

                for (var func:Object in callLaterHash) 
                {
                    hasRun = true;
                    var obj:CallLaterData = callLaterHash[func] as CallLaterData;
                    if (--obj.delay == 0) 
                    {
                        if (obj.params != null) 
                        {
                            (func as Function).apply(null, obj.params);

                        } else {
                            func();
                        }
                        delete callLaterHash[func];
                    }
                }

                if (hasRun == false) 
                { 
                    removeEventListener(Event.ENTER_FRAME, onFrame)
                }
            }


            protected static function getInstance():CallLater {

                if (_instance == null) 
                {
                    _instance = new CallLater()
                }

                return _instance;
            }
        }
    }


    internal class CallLaterData  
    {
        public var delay:int;
        public var params:Array;

        public function CallLaterData(p_delay:uint, p_params:Array)
        {
            delay = p_delay;
            params = p_params;
        }
    }