2

ActionScript 3 / Flex 3 - 向类添加自定义事件

假设我有以下事件:

import flash.events.Event;
public class SomeEvent extends Event
{
    public static const EVENT_ACTION:String = "eventAction";

    public function SomeEvent(type:String) {
        super(type);
    }

    override public function clone():Event {
        return new SomeEvent(this.type);
    }
}

...以及以下课程:

public class SomeClass extends EventDispatcher
{
    public function someFunction():void
    {
        dispatchEvent(new SomeEvent("eventAction"));
    }
}

显示“SomeClass”抛出“SomeEvent”的最佳方式是什么?我发现的唯一方法是用 [Event] 属性装饰“SomeClass”,如下所示:

[Event (name="eventAction", type="SomeEvent")]

这允许我实例化类并通过执行以下操作添加事件侦听器:

var someClassInstance:SomeClass = new SomeClass();
someClassInstance.addEventListener(SomeEvent.EVENT_ACTION, mycallbackmethod);

有一个更好的方法吗?将 [Event] 属性放在类上,后跟一些字符串文字只是感觉......错了。在此先感谢您的帮助!

4

2 回答 2

3

You're doing it right. Currently, the AS3 compiler allows only string literals in metadata. Constants cannot be used.

By the way, Adobe's public bug database has a feature request to allow ActionScript constants in metadata. Feel free to vote for it.

于 2009-01-16T23:38:09.903 回答
0

I know, I feel the same way; using the string literals does seem a bit wrong somehow. But to the best of my knowledge, from all the documentation I've seen and the talks I've attended and in reading the Flex source code, etc., it does appear that's the proper way to do it.

于 2009-01-16T21:06:03.487 回答