my custom event class
Code:
package app
{
import flash.events.Event
public class BoxEvent extends Event
{
public static const DRAWN:String = "drawn";
public function BoxEvent(type:String){
super(type);
}
public override function clone():Event{
return new BoxEvent(type);
}
}
}
my class that dispatches the event
Code:
package app {
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class Box extends Sprite
{
public function Box() {
addEventListener(Event.ADDED_TO_STAGE, PB);
}
public function PB(ev:Event):void {
//position the box then tell all the listeners that it is ready to be manipulated
this.x = newX;
this.y = newY
this.width = stage.stageWidth;
this.height = stage.stageHeight;
this.dispatchEvent(new BoxEvent(BoxEvent.DRAWN));
}
}
}
my class that creates a new box then waits until it has been positioned before interacting with it.
Code:
//BoxPlay class
headerBox = new Box();
headerBox.addEventListener(BoxEvent.DRAWN, addContent);
this.addChild(headerBox);
public function addContent(evt:Event):void {
//now I can play with the box
}
The code above does not work but if I dispatch the event not from my PB method (which is called when the box is added to the stage); but insted stick it in a seperate class method in my box class and call it from the BoxPlay class, it will work.
Code:
//BOX CLASS
public function rr():void {
this.dispatchEvent(new BoxEvent(BoxEvent.DRAWN));
}
Code:
//BOXPLAY CLASS
headerBox = new Box();
headerBox.addEventListener(BoxEvent.DRAWN, addContent);
this.addChild(headerBox);
headerBox.rr();
public function addContent(evt:Event):void {
//now I can play with the box
}
When I dispatched the event from my PB method it does not work but when dispatching from another method and calling that method from my main class it will work fine.
Another thing I tried was to keep the dispatch in the rr() method but call it from the PB() method in my box class insted of the main class, but this did not work either.
(when it does not work the event handler function simply does not get executed, there is no error);
I checked to make sure my listner was registered each time and it returned true.