Creating custom events

Posted by | Posted in Actionscript 3.0, Flash, Flex | Posted on 30-03-2008

I've come across the need to create custom events a few times. One nice thing you can do, which will greatly simplify your development efforts is create a basic, custom event that contains a data object, where you can store whatever data you might want to pass around in your event. The basic way to extend flash events involves

1. extending flash.events.Event

2. overriding the clone method

Optional but highly recommended is creating static constants for your event, along the lines of:
public static const MOUSE_DOWN:String = "mouseDown;

So, with the custom data field your class might look something like this:

 
package
{
     import flash.events.Event;
 
     public class MyEvent extends Event
     {
          public var data:Object;
 
          function MyEvent(type:String, bubbles:Boolean = false,
                         cancelable:Boolean = false)
          {
               super(type, bubbles, cancelable);
               data = new Object();
          }
 
          override public function clone():Event {
               var re:MyEvent= new MyEvent(type, bubbles, cancelable);
          }
     }
}
 

You can also add meta tags and override the toString function, but I found that these are unnecessary and maybe even a bit too much, but in case you're curious or want to implement these, you would do this

 
[Event(name="mouseDown", type="MyEvent"]
 

The benefit of this is that you can bind with this in Flex, but I like much better to write classes that I can use in both flash and flex, so this doesn't really help me much, but there it is.

Yet another bug with IE when loading XML with flash

Posted by | Posted in Flash | Posted on 13-03-2008

I kept having a security sandbox violation when loading files with flash, turns out using 127.0.0.1 instead of "localhost" worked it out... man... I mean common, this is basic