开发者

Dispatching events between classes

开发者 https://www.devze.com 2023-01-04 13:46 出处:网络
I have a custom event class public class FFTDrawEvent extends Event { public static const DRAW_EVENT:String = \"drawEvent\";

I have a custom event class

public class FFTDrawEvent extends Event {

    public static const DRAW_EVENT:String = "drawEvent";

    private var _param:Array = new Array();

    public function FFTDrawEvent(type:String, __param:Array, bubbles:Boolean=true, cancelable:Boolean=false) {
        _param = __param;
        super(type, bubbles, cancelable);
    }

    public function get param():Array {
        return _param;
    }
}

This event is dispatched by a class, that extends EventDispatcher:

public class ToneGenerator extends EventDispatcher {

 public function someFunction():void {

 this.dispatchEvent(new FFTDrawEvent(FFTDrawEvent.DRAW_EVENT,_param));
}

Another class listen to this event. This class is extending SpriteVisualElement.

public class SpectrumVisualizer extends `SpriteVisualElement`开发者_如何学Go:
{
    public function SpectrumVisualizer()
    {
        this.addEventListener(FFTDrawEvent.DRAW_EVENT, draw);
    }

Unfortunately this doesn't work. The event is dispatched (returns with true), but the Event listener is never triggered.

Both class (Dispatcher & Listener) are Child class of a MXML application. Also listen to the event in the parent MXML application doens't work. Listen to the event in the dispatching class itself somehow works.

I have to feeling that the EventDispatcher class is not the right one to dispatch events to a mxml application or respectivly AS classes, which extend/inherent from a MXML component class.

Anybody can help me with that?


You should listen for events on the dispatcher. In your case, an instance of ToneGenerator is dispatching the event and SpectrumVisualizer is listening - it wouldn't work.

Listen to the event in the dispatching class itself somehow works.

That's the only way it would work.

When you call

this.addEventListener(FFTDrawEvent.DRAW_EVENT, draw);

you are telling this object to call this.draw method whenever it (the this object) dispatches an event of type FFTDrawEvent.DRAW_EVENT. Change it to

toneGen.addEventListener(FFTDrawEvent.DRAW_EVENT, draw);

Now you are telling toneGen object to call this.draw method whenever it (the toneGen object) dispatches an event of type FFTDrawEvent.DRAW_EVENT.

You can also do the following from the parent mxml.

toneGen.addEventListener(FFTDrawEvent.DRAW_EVENT, specVis.draw);

Call specVis.draw method whenever the toneGen object dispatches an event of type FFTDrawEvent.DRAW_EVENT.


Martin,

The problem is that listener listens to itself instead of listening to event dispatcher. Here is the correct code:

public class SpectrumVisualizer extends SpriteVisualElement
{
    public function SpectrumVisualizer()
    {
        toneGenerator = new ToneGenerator();
        toneGenerator.addEventListener(FFTDrawEvent.DRAW_EVENT, draw);
        toneGenerator.someFunction();
    }

    private var toneGenerator:ToneGenerator;

    private function draw(event:FFTDrawEvent):void
    {
        trace("draw");
    }

}

This will help you to understand events.

P.S: If you tried to use bubbling, see the corresponding section in docs. Here it does not work because events "bubble" only in display list while ToneGenerator is not visual object (Sprite, MovieClip, ...).

0

精彩评论

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