dispatchEvent() trouble... help?

Hi!
I’m having trouble using the dispatchEvent() method to communicate between two custom classes. The problem is the Main class listens for an event that another class dispatches, but somehow it doesn’t ‘catch’ it…
Here is my main class:

package {
 import app.Thumb;
 
 import flash.display.Sprite;
 import flash.events.Event;
 public class TestLoadThumbs extends Sprite
 {
  private var _numThumbs:Number;
  private var _thumbCounter:Number
 
  public function TestLoadThumbs()
  {
   _numThumbs = 5;
   _thumbCounter = 0;
 
   addThumb()
  }
 
  private function addThumb():void {
   trace(_thumbCounter + '/' + _numThumbs);
 
   if (_thumbCounter < _numThumbs) {
    var thumb:Thumb = new Thumb();
    thumb.y = 50;
    thumb.x = 60 * _thumbCounter;
    thumb.addEventListener("thumbAdded", onThumbAdded, false, 0, true);
    addChild(thumb);
   }
  }
 
  private function onThumbAdded(evt:Event):void {
   _thumbCounter += 1;
   addThumb()
  }
 }
}

And here’s the Thumb class I use to create the thumb Shape:

package app
{
 import flash.display.Shape;
 import flash.display.Sprite;
 import flash.events.Event;
 public class Thumb extends Sprite
 {
  public function Thumb()
  {
   var shape:Shape = new Shape();
   shape.graphics.beginFill(0x000000);
   shape.graphics.drawRoundRect(0, 0, 50, 50, 15);
   shape.graphics.endFill();
   addChild(shape);
 
   dispatchEvent(new Event("thumbAdded"));
  }
 }
}

As you can see I trace the counter to check whether it gets updated at all. And of course I have the visual representation of the code.
I already found out that when I add the dispatchEvent() method to the Thumb instance in the main class, the event handler does load. I accomplished this by adding the following code after I added the event listener:

thumb.dispatchEvent(new Event("thumbAdded")); // works!

But obviously that’s not what I’m looking for.
I really hope someone can help me with this!
Thanks a lot in advance.

Hey there AmiKaze. :slight_smile: The problem here is that your listener is set up AFTER the event has been dispatched.


// The moment you create thumb, its constructor is called and your event is dispatched
var thumb:Thumb = new Thumb();

thumb.y = 50;
thumb.x = 60 * _thumbCounter;
 
// The listener is added, but your event was already dispatched when you made a new thumb!
thumb.addEventListener("thumbAdded", onThumbAdded, false, 0, true);
addChild(thumb);

I don’t know if this works for you, but if you just want to record that a thumb has been added to the display list, you could use the built-in event ADDED_TO_STAGE, which will dispatch when you addChild().


thumb.addEventListener(Event.ADDED_TO_STAGE, onThumbAdded, false, 0, true);

Ahhh, of course… Thanks a lot skineh!

My problem is solved!

Ah, one other thing. You should create a class for your events and define the event actions as a public static constant. Then if you change your mind about using the string “thumbAdded” later on, you update the constant and your code continues to work.

It’s the same way we use things like addEventListener(Event.ENTER_FRAME, […]); instead of addEventListener(“enterFrame”, […]); - same effect, but the former is much more preferred.

Here’s an example of a class that makes custom errors (of course it needs to be in a package, but that’s fairly straightforward :))…


public class MyCustomEvent extends EventDispatcher
{
    // High level event things
    public static const SOME_EVENT:String = 'someEvent';
[...]
}

Then you’d use it like so…


addEventListener(MyCustomEvent.SOME_EVENT, [...]);

Simple, a lot more readable and a lot more maintainable in the future.

Thanks for the tip dthought!
I’ll definitely look into it.