IdleSun's Weblog

October 9, 2008

Flex – When is the stage available?

Filed under: Flex — idlesun @ 6:21 pm

I wanted to capture all the key strokes and handle them once my Flex app is launched. I found that I need to add a handler to the stage and I tried with the creationComplete event of my Application. However, I couldn’t get what I want because the stage variable was STILL null. And then after a while I found that there is another event right after creationComplete. That is applicationComplete. Voila! I could access the stage and got what I wanted.

<mx:Application applicationComplete="onAppComplete()">
  <mx:Script><![CDATA[
    private function onAppComplete():void {
      if (this.stage != null)
      stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
    }
    private function onKeyUp(event:KeyboardEvent):void	{
      // handle key strokes
    }
  ]]><mx:Script>
  ...

By the way, I couldn’t resolve my problem without this: Flex Instantiation Life Cycle and Event Flow. Thanks Ted!

September 30, 2008

Wanna sleep in Flex?

Filed under: Flex — idlesun @ 10:58 pm

There is no sleep or delay function in ActionScript. Like JavaScript, you can use setTimeout() instead like this:

function trigger():void { setTimeout(doIt, 1000); }
function doIt():void    { Alert.show("done!"); }

Once you link the trigger() function to any event such as ‘click’, when the event happens alert box will show up after 1 sec.

There is also setInterval() and clearlnterval() functions you can use for repetition. However, it is recommended to use flash.utils.Timer class in that case.

Embed a XML file in Flex

Filed under: Flex,XML — idlesun @ 6:40 pm

Although you can embed XML string in the Flex code, you would want to make it as a seperate XML file and use it. So that you can get the benefits of editing XML data using XML editor. One of the easist way I found to embed external XML file is use [Embed] metadata tag. Here is the sample code:

[Embed(source="test.xml")]
var testXml:Class;

function init(): void {
  var xml:XML = testXml.data as XML;
  Alert.show(xml.toXMLString());
}

It is really simple, isn’t it? However, this technique has a couple of limitations like:

  • XML file can not have a XML declaration line like “<?xml version=”1.0″ encoding=”utf-8″?>”
  • To use international characters, save XML file in Unicode not in UTF-8

Sample Source: click right button on this link and select “Save Link As” to download the file and rename its extension to “zip”

September 15, 2008

Keep the generated in Flex

Filed under: Flex — idlesun @ 4:56 pm

Everybody who is using Flex (whether with SDK only or Flex Builder) knows MXML files are converted to Action Scripts. But I just wonder how many of us keep the generated code and take looks at them. It is very rare but sometimes you may get an error that is referring to the generated code.

In order to keep the genrated Action Script files use the “-keep” compiler argument, or in Flex Builder add it through the Project Properties dialog like this:

Adding -keep argument in Flex Builder

Adding -keep argument in Flex Builder

With this argument added, you will find the ‘generated’ folders where you have an application or module files. You definitly want to check what AS files are are generated from your MXML files.

Create a free website or blog at WordPress.com.