3

AS3 Flex 2 Simple component

So after playing with AS3 I thought I’d turn my attention to creating, or attempting to create, an AS3 / Flex component. So without further ado I unveil my SimpleButtonComponent (and it is just that LOL).

This is the Flex MXML.

The following code sets my custom namespace. This is the classpath to my AS3 components and it allows me to define a custom namespace called fgComp that is associated with it.

Next you will see that I have instantiated an instance of my SimpleButtonComponent here:

Finally here is the AS3 class SimpleButtonComponent.as that is used to actually make the button referenced in the above MXML:

As you can see from the AS3 class it doesn’t do anything spectacular, in fact all I have done is set the default label value to “My SimpleButtonComponent”. However as a starting point for AS3 based component creation if you are used to creating AS2 components you’ll notice a couple of slight changes.

1) There is no requirement for the symbolName properties for the class definition
2) The className property now forms part of the constructor as opposed to being stored as a static variable.
3) While it is detailed that you should call super() inside the constructor – I haven’t here and there appear to be no ill efects :P (we shall see though).

As a final note for all those still a bit confused with what packages are accessible to what language base. In Flex component creation we are back to the ‘old’ mx.* packages. Whereas in a pure AS3 development we would use the flash.* packages as the mx.* are not compatible anymore.

[UPDATE]
Actually you can use the Flex mx.* components in AS3 as detailed in the Adobe labs wiki. However it will be interesting to see what happens once ‘Blaze’ is out and how its components marry up to Flex 2’s…

Mike Jones

3 Comments

  1. Does anyone know how to add a UIComponent to an AS3 class.

    The following does not seem to work:

    package {

    import flash.display.Sprite;
    import mx.controls.Button;

    class myApp extends Sprite {

    function myApp() {
    var b:Button = new Button();
    addChild(b);
    }

    }

    }

  2. Your AS3 class has to extend UIComponent in order to add another UIComponent

Leave a Reply

Your email address will not be published. Required fields are marked *