AS3 Custom Event Dispatching


/ Published in: ActionScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.display.MovieClip;
  4.  
  5. /**
  6. * ...
  7. * @author Jeff Nehlsen
  8. */
  9. public class CustomEventExample extends MovieClip
  10. {
  11. public function CustomEventExample()
  12. {
  13. // Add an event handler for both types of events in my CustomEvent class
  14. addEventListener(CustomEvent.EVENT_DEFAULT, onDefaultEvent);
  15. addEventListener(CustomEvent.EVENT_CUSTOM, onCustomEvent);
  16.  
  17. // Dispatch the 'default' event
  18. dispatchEvent(new CustomEvent());
  19.  
  20. // Dispatch a 'custom' event.
  21. dispatchEvent(new CustomEvent(CustomEvent.EVENT_CUSTOM));
  22. }
  23.  
  24. // When a custom event is caught, a message will show.
  25. private function onCustomEvent(e:CustomEvent):void
  26. {
  27. trace("Custom event caught!");
  28. }
  29.  
  30. // When a default evnet is caught, this message will show.
  31. private function onDefaultEvent(e:CustomEvent):void
  32. {
  33. trace("Default event caught!");
  34. }
  35. }
  36.  
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.