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.