This post is a complement to an old post
Mon premier SWF9 gratos avec Flex SDK (in french).
When using MXML, a lot of
initializations are made in background (Application, UIComponent ...)
This post is a sample AS3 code
without any MXML that enables you using Flex Component in
pure AS3, initializing manually Flex Application.
Main.as:
package
{
import mx.core.Application;
import mx.events.FlexEvent;
import mx.styles.StyleManager;
import mx.styles.CSSStyleDeclaration;
import mx.controls.Button;
import mx.skins.halo.HaloBorder;
import flash.display.Bitmap;
public class Main extends Application
{
[Embed(source="../library/up.png")]
public var UpState : Class;
[Embed(source="../library/over.png")]
public var OverState : Class;
[Embed(source="../library/down.png")]
public var DownState : Class;
public function Main()
{
super();
this.layout = "absolute";
this.addEventListener(FlexEvent.CREATION_COMPLETE, handleComplete);
setupStyles();
}
private function setupStyles() : void
{
var style:CSSStyleDeclaration = new CSSStyleDeclaration();
style.setStyle( "borderSkin", mx.skins.halo.HaloBorder );
StyleManager.setStyleDeclaration( "Application", style, false );
style = new CSSStyleDeclaration();
style.setStyle( "textAlign", "left" );
style.setStyle( "fontAntiAliasType", "advanced" );
style.setStyle( "fontGridFitType", "pixel" );
style.setStyle( "paddingLeft", 10 );
style.setStyle( "paddingRight", 10 );
style.setStyle( "paddingTop", 5 );
style.setStyle( "paddingBottom", 5 );
style.setStyle( "horizontalCenter", 0 );
style.setStyle( "verticalCenter", 0 );
style.setStyle( "upSkin", UpState );
style.setStyle( "overSkin", OverState );
style.setStyle( "downSkin", DownState );
StyleManager.setStyleDeclaration( "Button", style, false );
}
private function handleComplete( event : FlexEvent ) : void
{
var button : Button = new Button();
button.label = "labelBtn";
addChild( button );
}
}
}
Compile it with
mxmlc.exe, included freely in the Flex SDK:
mxmlc Main.as -o Main.swf
Now you can use all Flex Application properties with the component.
This sample is for the Flex Button, but you can now try to use anothers Flex Components.
Download Sample
Notes :
- I use custom skin for the Flex Button (embeded assets) to display it. (haven't found yet how to use the default skin... :-( )
- All default Flex components behaviour are handled thanks to quick and manual Application and Component initialization