Actionscript 3.0 Component Development

Posted on Thursday 9 November 2006


Over the past week or so I've been posting snippets on creating MXML based components, and will continue to do so. That said I thought I'd post some articles on creating components in Actionscript 3.0 so you can see where the crossover exists and provide information that will, hopefully, provide a rounded view on which method of development suits each individual.

As with my first post on component development with MXML I thought I'd take the same code but re-engineer it in to AS 3.0 code so we can see the difference.


So first of all let's briefly review the MXML code of our extended ComboBox:

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml">
  3.     <mx:dataProvider>
  4.         <mx:String>Miss</mx:String>
  5.         <mx:String>Mrs</mx:String>
  6.         <mx:String>Mr</mx:String>
  7.         <mx:String>Dr</mx:String>
  8.     </mx:dataProvider>
  9. </mx:ComboBox>

As you can see it ha all the elements you'd expect but how would we reproduce it in AS 3.0? Well first of all it would make sense to include the package path as this will have more relevance as we move on. So for the component above it is called FGComboBox.mxml and it is located in com.flashgen.components.flex.controls. When we just create the initial Actionscript 3.0 class you'll see why this matter.

ACTIONSCRIPT:
  1. // Remember the package name is now external to the class identifier.
  2. package com.flashgen.components.flash.controls {
  3.     // Import our classes we need to create our component
  4.     import mx.controls.ComboBox;
  5.  
  6.     // Declare our class and provide the inheritance and interfaces
  7.     public class FGComboBox extends ComboBox
  8.     {   
  9.         private var _data   :Array = ["Miss", "Mrs", "Mr", "Dr"]
  10.        
  11.         // Define the constructor
  12.         public function FGComboBox()
  13.         {
  14.             // Call our parent
  15.             super();
  16.             applyData(_data);
  17.         }
  18.        
  19.         protected function applyData(elements:Array):void
  20.         {
  21.             // Set the dataProvider for the component
  22.             this.dataProvider = elements;
  23.         }
  24.     }
  25. }

If you want to try it out here is the MXML application file:

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:fgx="com.flashgen.components.flex.controls.*" xmlns:fgc="com.flashgen.components.flash.controls.*" viewSourceURL="srcview/index.html">
  3.     <fgx:FGComboBox id="fgxCombo" x="0" y="0" />
  4.     <fgc:FGComboBox id="fgcCombo" x="100" y="0" />
  5. </mx:Application>

placeholder for flash movieNow this is purely an example piece of code so it is unlikely you would really implement a component in this manner.

It will run and it has the same default values as the MXML version above it. The interesting thing to note is that once compiled you cannot tell the difference between the two versions. I know it is a very obvious thing to say but I thought I would be explicit.

Hopefully this has given a basis of comparison between the MXML and Actionscript 3.0 component development - albeit on the simple side.

Blatant Plug
Flash on the BeachCatch my talk on Flex 2 and Actionscript 3.0 component development at Flash on the Beach
December 4th-6th 2006, Brighton, UK

License

This work is published under a CC-GNU LGPL.



1 Comment for 'Actionscript 3.0 Component Development'

  1.  
    November 9, 2006 | 11:02 am
     

    Hey dude, The most interesting thing is the decision process that goes into deciding when to break something out into a component and how. No doubt so sort of “Best practice” will emerge.



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