AS3 Flex 2 Simple component

Posted on Thursday 24 November 2005


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.

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx :Application xmlns:mx="http://www.macromedia.com/2005/mxml" xmlns:fgComp="com.flashgen.examples.components.*">
  3.     <fgcomp :SimpleButtonComponent />
  4. </mx>

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.

XML:
  1. xmlns:fgComp="com.flashgen.examples.components.*"

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

XML:
  1. <fgcomp :SimpleButtonComponent />

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

ACTIONSCRIPT:
  1. package com.flashgen.examples.components
  2. {
  3.     import mx.controls.Button;
  4.  
  5.     public class SimpleButtonComponent extends Button
  6.     {      
  7.         private var _label:String = "SimpleButtonComponent";
  8.        
  9.         public function SimpleButtonComponent () {
  10.            
  11.             className = "SimpleButtonComponent";
  12.             this.label = _label;
  13.         }
  14.     }
  15. }

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...

License

This work is published under a CC-GNU LGPL.



3 Comments for 'AS3 Flex 2 Simple component'

  1.  
    December 23, 2005 | 9:02 am
     

    […] In addition to that there is another basic tutorial on Flex2/AS3 components here […]

  2.  
    Bryan Ledford
    July 27, 2006 | 11:22 pm
     

    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);
    }

    }

    }

  3.  
    Brendon Turk
    April 13, 2007 | 7:26 pm
     

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



Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.

Comments are moderated so please only submit once


RSS feed for comments on this post | TrackBack URI