Quick Tip: Constructor Of Property With Namespace

Some of you may not like the solution (including me) but sometimes you need a little hack to make things work for you. In my case I required a constructor of property declared under custom namespace but not defined (null). Normally, you would go for describeType(), but guess what, yeah it just does not describes properties with namespace other than public. While there was nothing better in my mind than passing some unexpected value and expect what happend I decided to go for it. Luckily an exception was thrown with correct constructor name.

Read the rest of this entry »

Quick tip: embedding in Flex

In Flex application, you can embed your assets into 3 places: ActionScript, MXML, CSS. To embed into ActionScript use following syntax:

[Embed(source="assets/image.png")]
public var imageClass:Class;

[Embed(source="assets/library.swf", symbol="symbol1")]
public var symbol1Class:Class;

// flash library symbol alternative
[Embed(source="assets/library.swf#symbol2")]
public var symbol2Class:Class;

To embed into MXML use:

<mx:Image source="@Embed('assets/image.png')"/>
<mx:Image source="@Embed('assets/library.swf', symbol='symbol1')"/>
<mx:Image source="@Embed('assets/library.swf#symbol2')"/>
<mx:Button skin="{null}"/>

To embed asset into CSS file use syntax:

Button 
{
    upSkin: Embed(source="assets/image.png");
    overSkin: Embed(source="assets/library.swf", symbol="symbol1");
    downSkin: Embed(source="assets/library.swf#symbol2");
    selectedUpSkin:ClassReference(null);
}

@font-face 
{
    font-family: Copacetix;
    src: url("assets/copacetix.ttf");
    unicode-range:
        U+0020-U+0040, /* Punctuation, Numbers */
        U+0041-U+005A, /* Upper-Case A-Z */
        U+005B-U+0060, /* Punctuation and Symbols */
        U+0061-U+007A, /* Lower-Case a-z */
        U+007B-U+007E; /* Punctuation and Symbols */
}

Embedding font to .as (.mxml):

[Embed(source="../arial.ttf", fontFamily="myArial")] 
public static const FONT_ARIAL:Class;
public static const FONT_ARIAL_NAME:String = "myArial";

[Embed(source="../fonts.swf", fontName="myCourier", fontWeight="bold|normal") ]
public static const FONT_COURIER:Class;
Font.registerFont(FONT_COURIER);

public function Test()
{
    var format:TextFormat = new TextFormat();
    format.font	= FONT_ARIAL_NAME;
    						
    var tf:TextField = new TextField();
    tf.embedFonts = true;
    tf.defaultTextFormat = format;
}

Read more about embedding assets into ActionScript best practices here. Read basics about embedding here.

Flex Error #1034: Type Coercion Failed: Cannot Convert *** to mx.core.IFlexDisplayObject

When you keep getting:

Flex Error #1034: Type Coercion Failed: Cannot Convert *** to mx.core.IFlexDisplayObject

… after using getDefinition() method, in my case:

// url = "../some.swf"
var request:URLRequest = new URLRequest(url);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, assetsComplete);
loader.load(request, context);
...
// assetsComplete()
var applicationDomain:ApplicationDomain = assetsLoader.contentLoaderInfo.applicationDomain;
var assetsClass:Class = applicationDomain.getDefinition("Assets") as Class;
...

The problem is, in fact, the security not coercion! Try move your .swf (the one that is beeing loaded) into the same directory where master .swf file is located.

Embedding assets wisely

You can embed various types of assets in your Adobe Flex applications (.jpeg, .png, .swf, .mp3, .svg, .ttf …). Embedded assets are compiled into the SWF file of your Flex application. They are not loaded at run time and you do not have to deploy the original asset files with your application. Read more about embedding assets on Adobe.

Basics first. How to make your symbol from Flash library visible for Flex (see images on the bottom of the artice):

  • 1. Create your symbol and linkage (library (Ctrl+L) / {your symbol} / Linkage…).
  • 2. Linkage Properties, set your class name that will be used in [Embed] meta.
  • 3. Export .swf file and make Assets.as.

Read the rest of this entry »