【自己寫個AS3組件庫】之 “擴充Sprite類,實現銷毀和UI重繪機制”

來源:互聯網
上載者:User
package {
    import org.casalib.core.IDestroyable;
    import org.mobit.ui.event.UIEvent;

    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.Event;

    /**
     * 顯示對象基礎類
     */
    public class MySprite extends Sprite implements IDestroyable {
        private var _events : Array;
        /**
         * 視圖是否發生變化
         */
        protected var _viewChange : Boolean;

        public function MySprite() {
            super();
            _events = [];
            if (this.stage) {
                this.invalidate(null);
            } else {
                addEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler);
            }
            addEventListener(Event.RENDER, this.onRender);
        }

        public static function destroyDisplayObj(obj : DisplayObject) : void {
            if (obj) {
                if (obj.parent)
                    obj.parent.removeChild(obj);
                obj = null;
            }
        }

        protected var _isDestroyed : Boolean;

        public function get destroyed() : Boolean {
            return this._isDestroyed;
        }

        public function destroy() : void {
            removeAllChildren(true);
            removeAllEventListener();
            this._isDestroyed = true;
        }

        public function addChildXY(child : DisplayObject, x : Number = 0, y : Number = 0) : void {
            addChild(child);
            child.x = x;
            child.y = y;
        }

        private function addedToStageHandler(event : Event) : void {
            this.invalidate(event);
        }

        protected function invalidate(event : Event) : void {
            if (stage != null) {
                stage.invalidate();
            }
        }

        private function onRender(event : Event) : void {
            if (_viewChange)
                this.updateDisplayList();
        }

        /**
         * 更新視圖方法
         */
        protected function updateDisplayList() : void {
            _viewChange = false;

            dispatchEvent(new UIEvent(UIEvent.ViewChanged));
        }

        /**
         * 移除所有子顯示對象
         */
        public function removeAllChildren(dispose : Boolean = false) : void {
            while (numChildren > 0) {
                var child : DisplayObject = removeChildAt(0);
                if (dispose) {
                    if (child is IDestroyable) {
                        (child as IDestroyable).destroy();
                    }
                }
            }
        }

        /**
         * 資料發生變化,需要重新繪製視圖
         */
        protected function viewChanged() : void {
            _viewChange = true;
            if (stage != null) {
                stage.invalidate();
            }
        }

        override public function addEventListener(type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void {
            _events.push({type:type, fun:listener});
            super.addEventListener(type, listener, useCapture, priority, useWeakReference);
        }

        override public function removeEventListener(type : String, listener : Function, useCapture : Boolean = false) : void {
            super. removeEventListener(type, listener, useCapture);
            for (var i : int = 0;i < _events.length;i++) {
                if ( _events[i].type == type && _events[i].fun == listener) {
                    _events.splice(i, 1);
                }
            }
        }

        public function removeAllEventListener() : void {
            var ev : Object;
            while (_events.length > 0) {
                ev = _events.pop();
                super.removeEventListener(ev.type, ev.fun);
            }
            _events = new Array();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.