Archive for the ‘Flash Components’ tag
UIMovieClip Custom Events
I’ve been using the Flex Integration Kit to create my own custom components in Flash for use in Flex lately. Coming from an all ActionScript (not necessarily MXML) background there are a few things to watch for. One of them being Custom Events in your newly created component.
In order for Flex to recognize your Custom Event you need to include the event meta data in your AS Class file. For example, in an AS setting your class could fire off an event like so:
private function updateHandler(e:MouseEvent):void { var evt:MyEvent = new MyEvent(MyEvent.VOLUME); evt.volume =//some volume value; this.dispatchEvent(evt); }
In an all ActionScript setting this works fine. However when you compile this UIMovieClip into a swc file and use it in a Flex Project, Flex has no way to recognize the MyEvent.VOLUME. In the Flex Editor it won’t appear as a code hint. If you manually force it into the MXML, you’ll get an error. In order to allow Flex to know of the event you will need to include some Event meta data at the top of your ActionScript Class. For example:
[Event (name="volume", type="com.knomedia.events.MyEvent")] public class VolumeControl extends UIMovieClip { //Class members here }
The meta data needs the ‘name’ of the Event. This is the string literal that the event constant holds. In this case MyEvent.VOLUME holds the value “volume”. The type attribute is the fully qualified name of your Custom Event Class. The placement of the meta data is important, it needs to be on the line above your class declaration. Once you have that in place you can compile your swc.
My only beef with the technique is that you need to use the actual string literal. I understand the need (for ease of use in MXML), but I am more comfortable using class constants of the Event Class… Anyway.
In MXML you can now set the volume attribute to listen for the event to fire:
<swc:VolumeControl id="vol" volume="{volumeHandler(event);}" />The switch from all ActionScript to MXML is pretty painless. Occasionally a small issue like this will make you feel stupid, but overall it isn’t a big jump.
Jason
Jason Madsen is a web developer who teaches OOP and Flex and acts as a Department Chair at a private university in Orlando, FL.