Not yet rated

Problem

Initializing a potentially large family of variables according to the value of a single index (for example, to define a game level numerically) in Flash Lite 1.x.

Solution

Lacking arrays, an efficient solution is to organize the variables into frames of a "deck" object, accessed via the call() method.

Detailed explanation

This is an example of a problem that could ordinarily be solved with a multidimensional array, accessed from a function. However, Flash Lite 1.x lacks both arrays and functions. Also, numbered "pseudo-array" variables rapidly get cumbersome when more than one variable dimension is required.

However, this can be structurally addressed by creating an offstage movieclip, where each frame contains a different set of potential values for the family of variables:

tellTarget("/") {
    // define level properties
    a = 30;
    b = 250;
    da = 7;
    db = 1;
    multi = false;
    posInit = 8;
}

In the above case, the variables are all set at the root level ("/"), for ease of access from other nested clips, but this is not essential.

With this deck constructed, and assuming it has been given an instance name of "deck", an entire variable set can be redefined from a simple numerical index:

currentLevel = 2;
call ("deck:" add currentLevel);

Be sure that the first frame of the deck contains a "stop()" command, with the other frames containing the variable initialization sets, or else the deck object will rapidly execute its own frames one after another when the movie runs.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes