PDA

View Full Version : Can actionscript do variable in variable?



Hiro
August 30th, 2004, 01:50 AM
I know in php etc its great with variables. You can use variables withinn variables to call another variable


e.g $somevar1, $somevar2

then $i = 1.

$$somevar$i (this will be treated as $somevar1)

Is it possible to do this in actionscript, because it saves one hell of alot of code.

Voetsjoeba
August 30th, 2004, 04:48 AM
Sure you can. It's called associative array referencing.



myvariable = "a nice variable";
firstpart = "my";
trace(this[firstpart+"variable"])


The expression in the [] will be evaluated first, and then used as name for the property/method/... to retrieve.

Hiro
August 30th, 2004, 07:58 PM
Awesome thanks!

Hiro
August 30th, 2004, 10:41 PM
Actually some really weird stuff is happening now, i wonder if you can tell me why.

I have a variable content0, content1 etc. Which have data loaded in the first frame.

When i click this button it loads the corresponding content.

if i do.


_root.mainText.scrollBox.htmlText = content0;

That works fine, yet if i do



action = "0";
_root.mainText.scrollBox.htmlText = this["content" + action];


it comes up undefined! Unless i put a content0 = content0;
before it.

Why would this happen it seems very silly to me!

Please note that content0 is a global variable from another frame, so maybe this has something to do with it?

Hiro
August 30th, 2004, 10:43 PM
bleh.. its funny how when you write a post it makes you think about it.

of course i needed to do

_global["content" + action]

NOT

this["content" + action]

because its a global variable.

Voetsjoeba
August 31st, 2004, 01:44 AM
Yep :)