Hi,
Currently when I want to dispatch an event to multiple instances of the same class I set up a listener for that event on the stage in my classes constructor, so that it is an umbrella type listener. Then dispatch an event targeted at that class (from outside the class) like so, myClassInstance.dispatchEvent(new Event(myClass.MY_EVENT,true)); which then is received and acted on by all instances of that class. This works fine.
However, it I am wondering if there is a better method for dispatching an event to all instances of a class? It seems, that the various approaches I try fail due to scope issues with the event only being broadcast to one instance of the class, not all.
Ie, something like this;
package {
import flash.events.*;
import flash.display.Sprite;
import flash.display.Stage;
public class testEvent extends Sprite {
public var counter:int=0;
public static const TEST_EVENT:String="testevent";
//constructor
public function testEvent(image:int) {
this.counter=image;
var testContent = new Sprite()
testContent.buttonMode=true;
testContent.useHandCursor=true;
testContent.graphics.beginFill(0x0000FF,100);
testContent.graphics.drawRect(10,10,10,10);
addChild(testContent)
addEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);
addEventListener(MouseEvent.CLICK,handleClick);//slide images
}
//added to stage event listener
private function addedToStageHandler(event:Event):void {
stage.addEventListener(testEvent.TEST_EVENT,indicate);
}
//handle a click
private function handleClick(e:MouseEvent):void {
dispatchEvent(new Event(testEvent.TEST_EVENT,true));
}
//custom event handler
private function indicate(e:Event):void {
if (this !== e.target) {
this.y = 20
} else{
trace(e.target.counter)
e.target.y = 10
}
}
}
}