PDA

View Full Version : Calling variables with the name from other variable's value


AndyMun
10-20-2008, 06:29 PM
hello, the title says it all.

if i have:

var1,var2,var3....var99,var100, each with a different value,
how can i call a var like: var<x> from the list above.

in php, it was possible ($var35="yoy";$m="var35";$$m).

runtime
10-20-2008, 06:32 PM
push them in an array and access them that way.

AndyMun
10-20-2008, 06:47 PM
the problem is that i load the from a file and i cann't load arrays.

CliveCarrington
10-21-2008, 03:52 PM
If, for example, the variables are on the _root, you can call them like this:

myNumber = 1;
trace (_root['var' + myNumber]);


This will be executed as if you wrote:

trace (_root.var1);


Furthermore, you can use this method for anything. For example, let's say you have, on the _root, two movieClips named "mc_red_ball" and "mc_green_ball". You can call them by this method this way:

myVar = 'red';
trace (_root['mc_' + myVar + '_ball']._width);
// traces the width in pixels of the movieClip mc_red_ball
myVar = 'green';
trace (_root['mc_' + myVar + '_ball']._width);
// traces the width in pixels of the movieClip mc_green_ball

This method has many more practical uses, and I personally use it a hell of a lot.

Hope I helped,
Clive.