Flash Lite Friday – Tip of the Day #3

September 21st, 2007 by Scott Janousek
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Flash Lite Friday - Tip of the Day #3

Hate using eval() statements to make arrays in Flash Lite 1.1, just for the simple fact that it looks silly (in today’s ActionScript standards).

For example:

[actionscript]
color_1 = “orange”;
color_2 = “green”;
color_3 = “blue”;
color_4 = “red”;

//– To dynamically access the elements in this pseudo-array,
//– you could use the following ActionScript code:

for (i = 1; i <=4; i++) {
trace (eval ("color_" add i));
}
[/actionscript]

Well, we do have a slightly better alternative …

However, it comes at the cost of using an offstage movieclip (so there is a bit of memory overhead).

So, if you really want to be picky about your code and efficiency (for instance, this might be of interest if you are really trying to optimize for memory size for a target device …) you’ll use eval() syntax above even though it looks silly in Flash Lite 2 (ActionScript 2.0+) standards.

However, baring the memory and scrutinizing, you can use a simple off-stage movieclip as a container for data, such as an array. Here’s how.

The trick is to use array style syntax notation to address an off-stage movie clip and throw variables into it, dynamically. Remember, in Flash Lite 1.1, there is no attachmovie().

For example:

[actionscript]
offstage_mc[ 0 ] = “a”; //– assigns letter “a” to zero index of a “movie clip” array
offstage_mc[ 1 ] = “b”; //– assigns letter “b” to 1st position …
offstage_mc[ 2 ] = “c”; //– You get the picture!
.
.
.
[/actionscript]

Basically you can think of it as an array, hash table, collection, or whatever data structure term you’re most comfortable using.

You can store your variables in there, instead of using the eval() statements.

Here’s a simple example:

Using a movieclip as an container for variables in Flash Lite 1.1

flftotd_0003.fla

Hope you enjoy this Flash Lite Friday “Tip Of The Day” provided by me.

Have a tip? Send me it. If I think it is practical, I’ll put it in the queue, and give you credit if/when it goes up.

3 Responses to “Flash Lite Friday – Tip of the Day #3”

  1. Marco Casario Says:

    ciao Scott,
    how’s gone ?

    Great initiative :) I’ll add it in the Flash Lite Friday Repository :) )
    See you soon,
    ciao
    marco

  2. Hayden Porter Says:

    one thing I ran into with eval() is that for FL1.1, the compiler allows us to use eval on both sides of an operator like:

    eval(“varname” add “1″) = eval(“anothervarname” add 2);

    but this code will generate an error if you publish to FL2

    set(“varname” add 1,”anothervarname” add 2) is allowed in both.

  3. Scott Janousek Says:

    Interesting about the double sided operation in FL2. Thanks for the post, Hayden.

Leave a Reply