0

我知道如何对一个类使用 addEventListener 来响应另一个类的按钮被点击。如果您想将它用于比这更通用的目的怎么办?如果你想用它来响应被调用的另一个类的成员函数之一怎么办?有没有这样的语法?谢谢!

编辑:请注意,我已经用谷歌搜索了答案。

4

3 回答 3

2

如果你想监听另一个类的成员函数调用,你需要那个函数调用来调度一个事件。这很简单...

监听类

addEventListener("customEvent", listenerFunc);

Dispatcher 类(扩展 EventDispatcher / 实现 IEventDispatcher)

dispatchEvent(new Event("customEvent"));

只要侦听器类在对象层次结构中高于调度程序类,这将完美地工作。如果没有,您可能希望使用某种 Global EventDispatcher 类并在其上注册所有侦听器。

于 2012-05-07T16:25:36.307 回答
1

您可以创建自己的事件并从其他类分派它们并在您的监听类中收听它们。这是一些代码

在 A 类中(假设它继承了 EventDispatcher)

public function classAMethod():void
{
   dispatchEvent(new Event("someCustomTypeForEvent"));
}

在 B 类中(假设它有对 A 类的引用)

public function classBMethod():void
{
   classA.addEventListener("someCustomTypeForEvent",customHandler);
}

public function customHandler(e:Event):void
{
   trace("handle event");
}
于 2012-05-07T16:27:28.753 回答
1

就像在 JAVA 中的 java.awt.Component 实例和所有扩展 java.awt.Component 的对象一样;在 AS3 中,您可以将侦听器添加到扩展 flash.display.Sprite 实例的所有对象,这些实例为您实现 IEventDispatcher 的方法...

因此,如果您有一个不扩展 flash.display.Sprite 的类,则必须扩展 EventDispatcher 才能将侦听器添加到您的实例并处理事件...

如果该类不能扩展 EventDispatcher,则必须实现 IEventDispatcher。

这是一个扩展 [class MovieClip] 的 [class MainClass]

这个 MainClass 实例创建:

[class ObjectA] 的一个实例,它扩展了 [class Object] 并实现了 IEventDispatcher,

[class ObjectB] 的一个实例,它扩展了 [class EventDispatcher]

这是使用扩展方法和实现方法的代码:我希望这个快速完成的示例对您有所帮助......(对不起我的英语,这不是我的母语。)

MainClass.as

package com
{
import flash.utils.getDefinitionByName;
import flash.display.MovieClip;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
import com.classes.ObjectA;
import com.classes.ObjectB;
import flash.events.Event;

public class MainClass extends flash.display.MovieClip
{

    private static const DEBUG:Boolean = true;
    private static var instance:MainClass;
    private static var instanceOfA:ObjectA;
    private static var instanceOfB:ObjectB;

    public function MainClass()
    {
        MainClass.debug("MainClass constructor called");
        MainClass.debug(getClassInformations(MainClass));
        MainClass.debug(getClassInformations(ObjectA));
        MainClass.debug(getClassInformations(ObjectB));

        instanceOfA = new ObjectA();
        instanceOfB = new ObjectB();
        instanceOfA.addEventListener(ObjectA.DO_SOMETHING_EVENT,onInstanceOfA_doSomething,false,0,false);
        instanceOfB.addEventListener(ObjectB.DO_SOMETHING_EVENT,onInstanceOfB_doSomething,false,0,false);
        instanceOfA.doSomething();
        instanceOfB.doSomething();
    }
    public static function onInstanceOfA_doSomething(e:Event):void
    {
        trace("An ObjectA has Dispatched An Event of type \"" + e.type + "\"" + " on " + e.target);
    }
    public static function onInstanceOfB_doSomething(e:Event):void
    {
        trace("An ObjectB has Dispatched An Event of type \"" + e.type + "\"" + " on " + e.target);
    }
    public static function getDebugMode():Boolean
    {
        return DEBUG;
    }

    public static function debug(string:String)
    {
        if (getDebugMode())
        {
            trace(string);
        }
    }
    public static function getClassInformations(someClass:Class):String
    {
        var clss:Object = null;
        var supClss:Object = null;

        clss = getDefinitionByName(getQualifiedClassName(someClass));
        try
        {
            supClss = getDefinitionByName(getQualifiedSuperclassName(someClass));
        }
        catch (e:ArgumentError)
        {
            // Has no superClass (ex:Object)
        }
        if (supClss != null)
        {
            return ("class " + clss + " extends " + supClss);
        }
        else
        {
            return ("class " + clss);
        }
    }
}
}

ObjectB.as中(最简单的方法):

package com.classes{
import com.MainClass;
import flash.events.EventDispatcher;
import flash.events.Event;

public class ObjectB extends EventDispatcher {
    public static var DO_SOMETHING_EVENT:String = "do_something_event";
    private var onDoSomethingEvent:Event = new Event(DO_SOMETHING_EVENT,false,false);
    public function ObjectB() {
        MainClass.debug("constructor ObjectB called");
    }
    public function doSomething():void{
        this.dispatchEvent(onDoSomethingEvent);
    }

}

}

在 ObjectA.as 中(必须实现接口 IEventDispatcher 的所有方法):

package com.classes
{
import com.MainClass;
import flash.events.IEventDispatcher;
import flash.events.EventDispatcher;
import flash.events.Event;

public class ObjectA implements IEventDispatcher
{
    public static var DO_SOMETHING_EVENT:String = "do_something_event";
    private var onDoSomethingEvent:Event = new Event(DO_SOMETHING_EVENT,false,false);
    private var dispatcher:EventDispatcher;
    public function ObjectA()
    {
        dispatcher = new EventDispatcher(this);
        MainClass.debug("constructor ObjectA called");
    }
    public function doSomething():void
    {
        this.dispatchEvent(onDoSomethingEvent);
    }
    public function addEventListener( 
    event_type:String, 
    event_listener:Function, 
    use_capture:Boolean = false, 
    priority:int = 0, 
    weakRef:Boolean = false 
    ):void
    {
        // implements addEventListener here
        dispatcher.addEventListener(event_type, event_listener, use_capture, priority, weakRef);
    }
    public function dispatchEvent(e:Event):Boolean
    {
        // implements dispatchEvent here
        return dispatcher.dispatchEvent(e);
    }
    public function removeEventListener(
    event_type:String, 
    event_listener:Function, 
    use_capture:Boolean = false
    ):void
    {
        // implements removeEventListener here
        dispatcher.removeEventListener(event_type, event_listener, use_capture);
    }
    public function hasEventListener(type:String):Boolean
    {
        // implements hasEventListener here
        return dispatcher.hasEventListener(type);
    }
    public function willTrigger(type:String):Boolean
    {
        // implements willTrigger here
        return dispatcher.willTrigger(type);
    }
}

}

请注意,如果您扩展 EventDispatcher,您可能还希望重写某些方法。在这种情况下,您必须使用“覆盖关键字作为:

public override function dispatchEvent (e:Event):Boolean {
        // a method of EventDispatcher may be overridden if needed !
        // do what you need HERE...
        return dispatchEvent(e);
    }

在 AS3 中,您必须指定 override 关键字,否则您将收到错误 1024:

“覆盖未标记为覆盖的函数。”

当您通过实现或扩展创建新的 EventDispatcher 时,您还可以为此对象指定其他参数和方法:

public function ListenerObject (v:View,m:Main) {
        dispatcher = new EventDispatcher(this);
        view = v;
        master = m;
    }
public function getView ():View {
        return view;
    }
public function getMain ():Main {
        return master;
    }

然后在回调方法中使用这些方法:

public function callback(e:Event):void{
    e.target.getView ();
    //...
}
于 2012-05-08T10:08:01.543 回答