tags:

views:

71

answers:

3

I have created a bunch of movie clips which all have similar names and then after some other event I have built up a string like:

var clipName = "barLeft42"

which is held inside another movie clip called 'thing'.

I have been able to get hold of a reference using:

var movieClip = Eval( "_root.thing." + clipName )

But that feels bad - is there a better way?

+2  A: 

Movie clips are collections in actionscript (like most and similar to javascript, everything is basically key-value pairs). You can index into the collection using square brackets and a string for the key name like:

_root.thing[ "barLeft42" ]

That should do the trick for you...

Ronnie
A: 

The better way, which avoids using the deprecated eval, is to index with square brackets:

var movieClip = _root.thing[ "barLeft42" ]

But the best way is to keep references to the clips you make, and access them by reference, rather than by name:

var movieClipArray = new Array();
for (var i=0; i<45; i++) {
 var mc = _root.thing.createEmptyMovieClip( "barLeft"+i, i );
 // ...
 movieClipArray.push( mc );
}

// ...

var movieClip = movieClipArray[ 42 ];
fenomas
A: 

You can use brackets and include variables within them... so if you wanted to loop through them all you can do this:

for (var i=0; i<99; i++) {
  var clipName = _root.thing["barLeft"+i];
}