Archive

Posts Tagged ‘actionscript’

creating flex components in Actionscript

August 1, 2009 Leave a comment

You can create a component in flex in two ways one with MXML we all know that and the other with Actionscript.The latter is a bit difficult but much more effective when it comes to understanding the code.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”absolute” creationComplete=”initApp()”>
<mx:Script>
<![CDATA[
import mx.controls.Button;
private function initApp():void
{
var mybutton:Button=new Button();
mybutton.label=”hi”;
mybutton.setStyle(“fontSize”,”16″);
this.addChild(mybutton);
}
]]>
</mx:Script>
</mx:Application>

The above example shows how to write an entire button component in AS.The function initApp() is called in the main application as creation complete event.All the you have in your mxml application can be called in your script section.

first you need to declare a variable for the button (var mybutton:Button)  then you need to call the constructor for the component (new Button();)

Note: The properties of the component should can be called using a .(dot) parameter(eg: mybutton.label=”hi”;)with the same name.

were as the style property of the component can be called using setStyle( )

(this.addChild(mybutton);)  in this line we are adding the our component to the container  AddChild();,so that our component is added to the main application.