Creating a custom Event really is as easy as 1, 2, 3.


Until recently although I knew you could do it, I had never really needed to create a custom event for a class that I had created. So when in a current project I needed to get my class to fire an event that would effect my document class I knew that this would be the perfect method and I was amazed at how simple and useful this was.

The Class that I created loads an external bitmap image and what I wanted to do was have it notify my document class when the bitmap was loaded so that it could trigger a particular animation for that sprite instance - there where many on the stage.

1: To start of with you need to create the Event, for this I created a class file called SpriteEvent which extends Event and added to it a static constant which would include the string for my event type:

Actionscript:
  1. public class SpriteEvent extends Event
  2.     {
  3.         public static const BITMAP_LOADED:String = "bitmapLoaded";
  4.        
  5.         public function SpriteEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
  6.         {
  7.             super( type, bubbles, cancelable );
  8.         }
  9.         public override function clone():Event
  10.         {
  11.             return new SpriteEvent(type, bubbles, cancelable);
  12.         }
  13.         public override function toString():String
  14.         {
  15.             return formatToString("MovieEvent", "type", "bubbles", "cancelable", "eventPhase");
  16.         }
  17.     }

You will notice two override functions which apparently always have to be included when you are creating custom events which include the specific return types. You can also include other custom data here that you would like returned once the event is finally triggered.

2: After this in your Sprite class file, once you get to the function that you would like to trigger this event - in my case it was after after a file had finished loading you add the dispatch event method to the function like so:

Actionscript:
  1. private function done(e:Event):void{
  2.             dispatchEvent(new SpriteEvent(SpriteEvent.BITMAP_LOADED));
  3.         }

3: Remember your class file will either need to be in the same package folder as your Event class or you make sure that is included in your imports. Anyway once this is all done you are then able to add an eventListener to your document class like so:

Actionscript:
  1. movie.addEventListener(SpriteEvent.BITMAP_LOADED, matLoaded);

This is of course waiting for the SpriteEvent to notify it when the bitmap has finished loading so that it can trigger the event listener.....easy!

You can leave a response, or trackback from your own site.

3 Responses to “Creating a custom Event really is as easy as 1, 2, 3.”

  1. Mo says:

    Why you use movie.addEventListener(SpriteEvent.BITMAP_LOADED, matLoaded); and not just addEventListener(SpriteEvent.BITMAP_LOADED, matLoaded);

  2. UltraVisual says:

    Your method would just add it to the stage…..anyway its just an example…….

Leave a Reply

Powered by WordPress | AT&T Cell Phones for Sale at iFreeCellPhones.com | Thanks to PalmPreBlog.com, MMORPGs and Online Shopping