开发者

Implementing IEventDispatcher in AS3

I haven't used the implements keyword before, and I've been trying to use it to implement the IEventDispatcher class to see if this would allow me to use addEventListener() in a class that extends Object (this is my understanding of what it's for - correct me if I'm wrong).

My class is like this:

package
{
    import flash.events.Event;
    import flash.events.IEventDispatcher;

    public class Thing extends Object implements IEventDispatcher
    {
        public function Thing()
        {
            addEventListener(Event.ENTER_FRAME, _handle);
        }

        private function _handle(e:Event):void
        {
            trace('a');
        }
    }
}

But this throws a stack of errors:

1180: Call to a possibly undefined method addEventListener.

1044: Interface method addEventListener in namespace flash.events:IEventDispatcher not implemented by class Thing.

1044:开发者_运维技巧 Interface method removeEventListener in namespace flash.events:IEventDispatcher not implemented by class Thing.

1044: Interface method dispatchEvent in namespace flash.events:IEventDispatcher not implemented by class Thing.

1044: Interface method hasEventListener in namespace flash.events:IEventDispatcher not implemented by class Thing.

1044: Interface method willTrigger in namespace flash.events:IEventDispatcher not implemented by class Thing.

Where have I gone wrong?


to add to jeremymealbrown's answer, Adobe provides an example in the documentation:

import flash.events.IEventDispatcher;
import flash.events.EventDispatcher;
import flash.events.Event;

class DecoratedDispatcher implements IEventDispatcher {       
    private var dispatcher:EventDispatcher;

    public function DecoratedDispatcher() {
        dispatcher = new EventDispatcher(this);
    }

    public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
        dispatcher.addEventListener(type, listener, useCapture, priority);
    }

    public function dispatchEvent(evt:Event):Boolean{
        return dispatcher.dispatchEvent(evt);
    }

    public function hasEventListener(type:String):Boolean{
        return dispatcher.hasEventListener(type);
    }

    public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
        dispatcher.removeEventListener(type, listener, useCapture);
    }

    public function willTrigger(type:String):Boolean {
        return dispatcher.willTrigger(type);
    }
}


The problem is just as the error message says. You are not implementing the methods defined in the IEventDispatcher interface. If you want to use implements you must explicitly define the functions declared in the interface. That means you actually have to write those functions in your class. On the other hand if you don't want to implement custom functionality, you can just extend EventDispatcher.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜