views:

50

answers:

2

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`:
{
    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?

+1  A: 

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, ...).

Maxim Kachurovskiy
That bubbling doesn't work on nonvisual objects is too bad. That was a nice way for me to loose coupling my classes.
Martin
+2  A: 

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.

Amarghosh
I did it in that way. But on the other side, I'm loosing my loose coupling of the two classes. Now ToneGenerator is an instance of SpectrumVisualizer. But anyway it works that way and the result is more important. Thanks.
Martin
@Martin "Now ToneGenerator is an instance of SpectrumVisualizer" why would you do that? leave them independent and add listeners from the parent that contains both of them. Events are in a sense part of the public interface of the class - so there's nothing wrong about doing it.
Amarghosh
Of course, stupid me. Did that and now I'm happy again. :-)
Martin