AS3谁删除了孩子?

时间:2017-09-15 18:40:53

标签: actionscript-3 removechild

我有遗产项目。有时我的一个窗户被从舞台上移除。我创建了测试项目来模拟子项链的情况.mc1.mc2.mc3.mc4

mc4是MovieClipWithEvents自定义类

之后我放了removeChild(mc1);行 在mc4中我也收到了REMOVED_FROM_STAGE,但是我不知道其中一个父项被删除了。 跟踪中的globalVisibleList方法显示:

// mc4添加到mc3

[trace] onAddedToStage mc4 mc3 mc4 mc4              

// mc1从root中删除,但是event.target显示mc4已从mc3中删除

[trace] onRemovedFromStage mc4 mc3 mc4 mc4      

//尽管我们知道mc1已从root删除并收到REMOVED_FROM_STAGE

,但所有父母仍然有父母并且可见
[trace] [object MovieClipWithEvents2], mc4, true
[trace] [object MovieClip], mc3, true
[trace] [object MovieClip], mc2, true
[trace] [object MovieClip], mc1, true
[trace] [object Test_VisibleEvent2], root1, true
[trace] [object Stage], null, true                  

//下一行trace("*************\n" + mc4.globalVisibleList(mc4)); 一切都好,mc1现在不是root的孩子

[trace] *************
[trace] [object MovieClipWithEvents2], mc4, true
[trace] [object MovieClip], mc3, true
[trace] [object MovieClip], mc2, true
[trace] [object MovieClip], mc1, true               

Test_VisibleEvent2

package {
    import display.MovieClipWithEvents2;

    import flash.display.MovieClip;
    import flash.display.Sprite;

    public class Test_VisibleEvent2 extends Sprite {
        private var mc1:MovieClip;
        private var mc2:MovieClip;
        private var mc3:MovieClip;
        private var mc4:MovieClipWithEvents2;
        public function Test_VisibleEvent2() {
            mc1 = new MovieClip();
            mc2 = new MovieClip();
            mc3 = new MovieClip();
            mc4 = new MovieClipWithEvents2();

            mc1.name = "mc1";
            mc2.name = "mc2";
            mc3.name = "mc3";
            mc4.name = "mc4";

            addChild(mc1);
            mc1.addChild(mc2);
            mc2.addChild(mc3);
            mc3.addChild(mc4);

            removeChild(mc1);
            trace("*************\n" + mc4.globalVisibleList(mc4));

        }
    }
}

MovieClipWithEvents2

package display {
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.Event;

    public class MovieClipWithEvents2 extends MovieClip {
        public function MovieClipWithEvents2() {
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
        }

        protected function onAddedToStage(event:Event):void {
            trace("onAddedToStage", name, parent.name, event.target.name, event.currentTarget.name);
        }

        protected function onRemovedFromStage(event:Event):void {
            trace("onRemovedFromStage", name, parent.name, event.target.name, event.currentTarget.name);
            trace(globalVisibleList(this));
        }

        public function globalVisibleList(displayObject:DisplayObject):String {
            var result:String = "";
            var checking:DisplayObject = displayObject;
            while(checking){

                result += getNameAndVisible(checking);

                if(checking.parent){
                    checking = checking.parent;
                }else{
                    return result;
                }
            }
            return result;// to make Idea happy
        }

        public function getNameAndVisible(displayObject:DisplayObject):String {
            return displayObject + ", " + displayObject.name + ", " + displayObject.visible + "\n";
        }
    }
}

所以我有两个问题:

  1. 为什么mc4会收到REMOVED_FROM_STAGE,但是所有的父母链都会在舞台上不间断?

  2. 如何确定删除哪个父项?

0 个答案:

没有答案