简体   繁体   English

事件ADDED_TO_STAGE执行次数为3次

[英]Event ADDED_TO_STAGE executed more times as3

I am really curios why is this happening. 我真的好奇为什么会发生这种情况。 I created two objects. 我创建了两个对象。 One is child of another. 一个是另一个孩子。 I registered both with event listener ADDED_TO_STAGE. 我在事件监听器ADDED_TO_STAGE中注册了两个。 Method onAdded in classB is executed twice. classB中的onAdded方法执行两次。

Why is this happening and how can i prevent this behaviour ? 为什么会发生这种情况?如何防止这种行为?

thanx for answer thanx回答

public class ClassA extends Sprite 
{
        public function ClassA () 
        {
            this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
        }

        private function onAdded(e:Event):void
        {
            trace("ON ADDED 1");
            var classB : ClassB = new ClassB();
            addChild(classB);
        }
}

public class ClassB extends Sprite 
{
        public function ClassB () 
        {
            this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
        }

        private function onAdded(e:Event):void
        {
            trace("ON ADDED 2");
        }
}

OUTPUT: ON ADDED 1 ON ADDED 2 ON ADDED 2 OUTPUT:ON ADDED 1 ON ADDED 2 ON ADDED 2

From here : There are two similar events: 这里 :有两个类似的事件:

Event.ADDED_TO_STAGE
Event.ADDED

There are difference between them: 它们之间有区别:

ADDED 添加

gets dispatched when the listening DisplayObject is added to another DisplayObject (no matter if it is a Stage object or not). 将侦听DisplayObject添加到另一个DisplayObject时(无论它是否为Stage对象),都会调度它。 Also it gets dispatched if any other DisplayObject is added to the listening DisplayObject. 如果将任何其他DisplayObject添加到侦听DisplayObject,它也会被调度。

ADDED_TO_STAGE ADDED_TO_STAGE

gets dispatched when the listening DisplayObject is added to the stage OR to any other DisplayObject which is added to stage. 将侦听DisplayObject添加到舞台或添加到舞台的任何其他DisplayObject时调度。


In your case it dispatches two times: 在你的情况下,它发送两次:

1) ClassB is added to ClassA that already added to the Stage. 1)ClassB被添加到已添加到舞台的ClassA中。

2) ClassB is added to the Stage. 2)ClassB被添加到舞台上。

It's kinda low level API. 这是一种低级API。 You can provide custom logic in case of .parent is Stage or not. 如果.parent是Stage,则可以提供自定义逻辑。 Basically ones you know that, you don't really need to listen this and you can call: 基本上你知道的,你真的不需要听这个,你可以打电话:

this.removeEventListener(Event.ADDED_TO_STAGE, onAdded);

to prevent calling onAdded twice. 防止两次调用onAdded。

Another way to prevent that is adding classB while constructing classA: 防止这种情况的另一种方法是在构造classA时添加classB:

private classB:ClassB = new ClassB();
public function ClassA () 
{
    addChild(classB);
    this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
}

It is because of event bubbling. 这是因为事件冒泡。

You should be removing the event listener once onAddedToStage is called, so the first statement inside: 你应该在调用onAddedToStage删除事件监听器,所以内部的第一个语句:

private function onAdded(e:Event):void

should be 应该

removeEventListener(Event.ADDED_TO_STAGE, onAdded);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM