【自己写个AS3组件库】之 “扩展Sprite类,实现销毁和UI重绘机制”

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();
        }
    }
}

转载于:https://www.cnblogs.com/DavidFan/archive/2012/05/29/2523575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值