AS3 将事件分派给显示列表上不相关的对象

发布于 2024-08-11 07:36:15 字数 310 浏览 2 评论 0原文

因此,事件会在显示列表中冒泡。如果这就是您想要的,那就太好了。但是,如果舞台上有不相关的对象需要侦听彼此的事件怎么办?一个简化的示例:

var objA = new ObjA;
addChild(objA);

var objB = new ObjB;
addChild(objB);

var objC = new ObjC;
objB.addChild(objC);

对象 B 可以侦听对象 C 调度的事件。但我还需要对象 A 侦听对象 C 调度的事件。此外,这些对象是在不同的类中创建的,因此我无法对彼此的引用进行硬编码。解决方案?

So, events bubble up the display list. That's great if that's what you want. But what if you have objects on the stage that are not related that need to listen for events from each other? A simplified example:

var objA = new ObjA;
addChild(objA);

var objB = new ObjB;
addChild(objB);

var objC = new ObjC;
objB.addChild(objC);

Object B can listen for events dispatched by object C. But I also need object A to listen for events dispatched by object C. Also, these objects are created in different classes so I can't hard code references to each other. Solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终遇你 2024-08-18 07:36:15

解决方案:如果您的事件正确冒泡,您应该能够从扩展 DisplayObject 且位于显示链上的任何类在舞台上设置侦听器。

注意:如果您只听标准事件,您将无法真正分辨出它们来自哪里,除非您知道特定事件只能来自一个“地方”。

修复:在这种情况下,最好分派自定义事件。


在任何扩展 DisplayObject 的类中

stage.addEventListener(MyAwesomeEvent.SOMETHING_SUPER_SWEET, sweetHandler)

solution: If your events are properly bubbling, you should be able to set a listener on the stage from any class that extends DisplayObject and is on a display chain.

catch: If you just listen for the standard events you are not going to really be able to tell where they are coming from unless you know that a particular event can only be coming from one "place".

fix: In cases like this it's best to dispatch custom events.


inside any class that extends DisplayObject

stage.addEventListener(MyAwesomeEvent.SOMETHING_SUPER_SWEET, sweetHandler)
仙女山的月亮 2024-08-18 07:36:15

如果您使用 addEventListener() 的最后一个参数,则无需删除侦听器,该参数应设置为 true
这意味着您的侦听器将使用弱引用,因此不会阻止 GC 收集侦听器的所有者。

there's no need to remove your listeners, if you use the last parameter of addEventListener(), which should be set to true
it means that your listener will use weak reference, and thus will not prevent GC from collecting the owner of the listener.

碍人泪离人颜 2024-08-18 07:36:15

那么,您可能可以创建一个 Singleton EventDispatcher 并在所有类中使用它来调度事件。

package <WHATEVER>
{
  import flash.events.*;
  public class SingletonDispatcher extends EventDispatcher {
    private static var dispatcher:SingletonDispatcher = null;
    public function SingletonDispatcher (enforcer:SingletonEnforcer) : void {
      if (!enforcer) {
       throw new Error("Direct instatiation is not allowed");
      }
      return;
    }// end function
    public static function GetInstance() : SingletonDispatcher {
      if (!dispatcher) {
        dispatcher= new SingletonDispatcher (new SingletonEnforcer());
      }// end if
      return dispatcher;
    }// end function
  }
}
class SingletonEnforcer {}

然后你可以在所有类中使用,例如:

public class C {
...
SingletonDispatcher.GetInstance().dispatchEvent(new SomeEvent()); // instead of this.dispatchEvent
...
}

在类 A 中,你可以听它:

public class A {
  ...
  SingletonDispatcher.GetInstance().addEventListener(SomeEvent.UNREACHABLE_EVENT, thankGod);
  ...
}

Then probably, you can make a Singleton EventDispatcher and use it in all classes to dispatch the event.

package <WHATEVER>
{
  import flash.events.*;
  public class SingletonDispatcher extends EventDispatcher {
    private static var dispatcher:SingletonDispatcher = null;
    public function SingletonDispatcher (enforcer:SingletonEnforcer) : void {
      if (!enforcer) {
       throw new Error("Direct instatiation is not allowed");
      }
      return;
    }// end function
    public static function GetInstance() : SingletonDispatcher {
      if (!dispatcher) {
        dispatcher= new SingletonDispatcher (new SingletonEnforcer());
      }// end if
      return dispatcher;
    }// end function
  }
}
class SingletonEnforcer {}

Then you can use in the all classes like:

public class C {
...
SingletonDispatcher.GetInstance().dispatchEvent(new SomeEvent()); // instead of this.dispatchEvent
...
}

And in class A, you can listen to it:

public class A {
  ...
  SingletonDispatcher.GetInstance().addEventListener(SomeEvent.UNREACHABLE_EVENT, thankGod);
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文