you call the event on the constructor... you add the listener to the event after you make a new instance of that class, so your adding the listener AFTER dispatching the event
On Thu, Jul 9, 2009 at 1:55 PM, Merrill, Jason < [email protected]> wrote: > You have "testInitialize" as both a constant value and the name of an > event. > > I would follow this format instead: > > ===================== > Write the custom event: > > package > { > import flash.events.Event; > > public class MyEvent extends Event > { > public static var SOME_EVENT:String = "some_event"; > > public function MyEvent(type:String, > bubbles:Boolean=false, cancelable:Boolean=false) > { > super(type, bubbles, cancelable); > > } > > public override function clone():Event > { > return new MyEvent(type, bubbles, cancelable); > } > > } > > } > > ===================== > Dispatch the event: > > package > { > import flash.events.EventDispatcher; > import MyEvent; > > public class MyClass extends EventDispatcher > { > > public function MyClass() > { > > } > > public function initialize():void > { > dispatchEvent(new MyEvent(MyEvent.SOME_EVENT)); > } > > } > > } > > ===================== > Listen for the event: > > var myClass:MyClass = new MyClass(); > myClass.initialize(); > myClass.addEventListener(MyEvent.SOME_EVENT, onSomeEvent); > > ===================== > Respond to the event: > > private function onSomeEvent(event:MyEvent):void > { > trace("event happened.") > } > > > > Jason Merrill > > Bank of America Global Learning > Shared Services Solutions Development > > Monthly meetings on the Adobe Flash platform for rich media experiences > - join the Bank of America Flash Platform Community > > > > > -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of ACE > Flash > Sent: Thursday, July 09, 2009 1:44 PM > To: Flash Coders List > Subject: [Flashcoders] dispatch a custom Event in a simple class file > > Hi there, > > I was working on my class file, but the event didn't get called > successfully. Anyting I am missing something? > > Thank you > > > ======================== > > var mc:MyClass = new MyClass(); > mc.addEventListener ( "testInitialize" , onINIT ); > > > function onINIT ( e:Event ) > { > trace("INIT was called"); > } > > > > > > 1. package > 2. { > 3. import flash.events.Event; > 4. import flash.events.EventDispatcher; > 5. > 6. [Event(name="testInitialize", type="flash.events.Event")] > 7. public class MyClass extends EventDispatcher > 8. { > 9. > 10. private const TEST_INITIALIZE :String = > "testInitialize"; > 11. > 12. public function MyClass () > 13. { > 14. initialize(); > 15. } > 16. > 17. private function initialize():void > 18. { > 19. ...... > 20. dispatchEvent ( new Event( TEST_INITIALIZE ) ); > 21. } > 22. } > 23. } > _______________________________________________ > Flashcoders mailing list > [email protected] > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > _______________________________________________ > Flashcoders mailing list > [email protected] > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > _______________________________________________ Flashcoders mailing list [email protected] http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

