Bryan
Bryan

Reputation: 17581

Dynamically accessing nested movie clips in flash actionscript 2

I have a nested movie clip instance that I want to access. The path to the movie clip is defined by two variables ( similar to a row and column).

I am already dynamically accessing the parent movie clip like this:

eval("row" + ActiveRow)

Now I want to access one of row(#)'s children called let(#) dynamically.

Here are my best guesses at accomplishing the task (neither one works):

var i:number;

eval("row" + ActiveRow + ".let" + i) or eval("row" + ActiveRow).eval("let" + i)

Thanks a lot for your effort and possible solution..

Upvotes: 2

Views: 8261

Answers (3)

Michael Hoisie
Michael Hoisie

Reputation:

First of all, it seems like you're using a tabular data structure, so one easy way would be to create a two-dimensional array and just store the movie clips in there. Then you can do lookups by index.

Alternatively, you name each of your movie clips (using the name property), and use getChildByName.

I.E getChildByName("row"+i).getChildByName("column"+i).

Upvotes: 0

johnny
johnny

Reputation: 14092

Once you access the parent movie clip, simply index into the child. ActionScript 2 does not require you to use the eval function for looking up dynamic properties. Simply use the object and the [] (array) operators to index the desired variable.

If your "row" objects are in the root of the current movie clip, you could simply use _root[ "row" + ActiveRow ][ "let" + i ].

However, since you already have the initial movieclip via eval("row"+ActiveRow), you can use this object to get the next level down. For example, eval("row" + ActiveRow)[ "let" + i ].

Flash borrows heavily from JavaScript, and like JavaScript, every object is essentially a hash table. Using the dot operator is equivalent to using the [] (array) operator with a fixed string.

Upvotes: 1

James Hay
James Hay

Reputation: 12700

you could try

this["row" + ActiveRow]["let" + i]

What would be better though is if when you create the instances you put them in an array... so maybe

var rowClips : Array = [];

for (var i : int = 0; i < 10; i++)
{
     var row : MovieClip = this.createEmptyMovieClip("row" + i, i);

     rowClips.push(row);
}

you can then call it by

rowClips[i];

Obviously depending on the situation there could be different logic to adding your MovieClips to an Array but essentially it's a much nicer way to store references to your MovieClips.

Upvotes: 8

Related Questions