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?
对象B可以侦听由对象C调度的事件。但是我还需要对象A来侦听由对象C调度的事件。此外,这些对象是在不同的类中创建的,因此我不能硬编码彼此的引用。解?
3 个解决方案
#1
0
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.
解决方案:如果您的事件正在冒泡,您应该能够在任何扩展DisplayObject并位于显示链上的类中设置舞台上的侦听器。
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".
catch:如果您只是倾听标准事件,那么除非您知道特定事件只能来自一个“地方”,否则您无法确定它们来自何处。
fix: In cases like this it's best to dispatch custom events.
修复:在这种情况下,最好派遣自定义事件。
inside any class that extends DisplayObject
在任何扩展DisplayObject的类中
stage.addEventListener(MyAwesomeEvent.SOMETHING_SUPER_SWEET, sweetHandler)
#2
0
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.
如果使用addEventListener()的最后一个参数,则不需要删除侦听器,该参数应设置为true,这意味着您的侦听器将使用弱引用,因此不会阻止GC收集侦听器的所有者。
#3
0
Then probably, you can make a Singleton EventDispatcher
and use it in all classes to dispatch the event.
然后,您可以创建一个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 {}
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:
在A班,你可以听听它:
public class A {
...
SingletonDispatcher.GetInstance().addEventListener(SomeEvent.UNREACHABLE_EVENT, thankGod);
...
}
#1
0
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.
解决方案:如果您的事件正在冒泡,您应该能够在任何扩展DisplayObject并位于显示链上的类中设置舞台上的侦听器。
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".
catch:如果您只是倾听标准事件,那么除非您知道特定事件只能来自一个“地方”,否则您无法确定它们来自何处。
fix: In cases like this it's best to dispatch custom events.
修复:在这种情况下,最好派遣自定义事件。
inside any class that extends DisplayObject
在任何扩展DisplayObject的类中
stage.addEventListener(MyAwesomeEvent.SOMETHING_SUPER_SWEET, sweetHandler)
#2
0
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.
如果使用addEventListener()的最后一个参数,则不需要删除侦听器,该参数应设置为true,这意味着您的侦听器将使用弱引用,因此不会阻止GC收集侦听器的所有者。
#3
0
Then probably, you can make a Singleton EventDispatcher
and use it in all classes to dispatch the event.
然后,您可以创建一个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 {}
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:
在A班,你可以听听它:
public class A {
...
SingletonDispatcher.GetInstance().addEventListener(SomeEvent.UNREACHABLE_EVENT, thankGod);
...
}