About

<#TEMPLATE_INCLUDE_NINEPAGE_ABOUTME#>
  • Jan

    11

    回顾 Starling2 —— 移除方法中 dispose 参数的含义

    • 0 Comments
    • Flash Platform
    • 发布:古树悬叶
    • 引用:0
    • 浏览:

    Starling 移除方法中 dispose 的功能:移除对象的同时会移除它的全部侦听器、以及移除子级对象的全部侦听器,释放对象与子对象的所有 GPU 显存。

    package
    {
        import starling.display.Quad;
        import starling.display.Sprite;
        import starling.events.Event;
        import starling.events.TouchEvent;

        public class Game extends Sprite
        {
            private var sp:Sprite = new Sprite();

            private var sub1:Sprite = new Sprite();
            private var sub2:Sprite = new Sprite();

            private var sub1sub:Sprite = new Sprite();

            public function Game()
            {
                super();

                this.addChild(sp);

                sp.addChild(sub1);
                sp.addChild(sub2);

                sub1.addChild(sub1sub);

                sp.addEventListener(TouchEvent.TOUCH, touchHandler);

                sub1.addEventListener(TouchEvent.TOUCH, touchHandler);
                sub2.addEventListener(TouchEvent.TOUCH, touchHandler);

                sub1sub.addEventListener(TouchEvent.TOUCH, touchHandler);
                sub1sub.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

                trace(sp.hasEventListener(TouchEvent.TOUCH));//true
                trace(sub1.hasEventListener(TouchEvent.TOUCH));//true
                trace(sub2.hasEventListener(TouchEvent.TOUCH));//true
                trace(sub1sub.hasEventListener(TouchEvent.TOUCH));//true
                trace(sub1sub.hasEventListener(Event.ENTER_FRAME));//true

                trace("========================");

                sub1.addChild(new Quad(100,100,0xff));//这个 Quad 显示对象会在以下方法被调用时被销毁
                sub1.removeFromParent(true);//一个对象从父级移除时设置 dispose 参数为 true

                sp.addChild(sub1);

                trace(sp.hasEventListener(TouchEvent.TOUCH));//true
                trace(sub1.hasEventListener(TouchEvent.TOUCH));//false
                trace(sub2.hasEventListener(TouchEvent.TOUCH));//true
                trace(sub1sub.hasEventListener(TouchEvent.TOUCH));//false
                trace(sub1sub.hasEventListener(Event.ENTER_FRAME));//false

                sp.removeChild(sub1sub);//这个比较奇葩,如果被移除的对象不是父级的子对象,它也不会抛出异常
            }

            private function enterFrameHandler(e:Event):void
            {
            }

            private function touchHandler(e:TouchEvent):void
            {
            }

        }

    }

    注意:上面的 Quad 对象的显存被释放掉了;另一个奇葩现象是一个容器在移除另一个对象时,即使不是“父子”关系(而是“子父”关系)、或两对象没有任何“父”与“子”关系,它也不会像原生 API 那样抛出异常。

     

    • Tags:
    • 2017 / 1 / 11
    相关文章:
      Post Comment.

      发表评论