Groups | Blog | Home
all groups > flash actionscript > november 2004 >

flash actionscript : refer to mc by variable name


tralfaz
11/25/2004 1:33:15 PM
There are two conversion methods in Flash to convert from a string to
an instance name...
If the instance name is a :

something = "a";
_root[something]._x += 5;
// or the older way..
eval(something)._x += 5;

Important note.. there is no dot between the _root and the left
bracket [
tralfaz


[quoted text, click to view]

TUUK
11/25/2004 9:21:20 PM
Hi everyone, I'm having problems with the syntax of something. Say I had a
movie clip on the stage called 'a' and I had a variable called 'name' who's
contents were also 'a'. Why can't i find the x/y of 'a' on stage through a
script by writing _root.name._x? If you have a variable that is the same as the
instance name of a mc on stage, how do you use the variable contents to refer
to the movie? I hope people understand what I'm trying to say and can offer
advice on this. thanks again everyone
TUUK
11/26/2004 2:25:27 PM
Absolutley spot on mate. That's exactly what I needed. You've saved me a few headaches with that one.

thanks a ton :)
Jeckyl
11/27/2004 1:36:37 AM
[quoted text, click to view]

the first method (using [] operator) looks up the value expression as if it
were written in dot notation. so
_root[something]
is identical to
_root.a
when something has the value 'a'

eval will evaluate will look up value expression as if it was written as a
simple, unadorned variable. so
eval(something)
is identical to
a
EXCEPT that you cannot use it (directly) on the LHS of an assignment. It is
only the value of the variable that eval returns.

There is a difference here, in that eval will give you the value of a local
variable. You cannot get at these using the [] notation.

So eval(something) will only be the same as _root[something] if we are in
the context of the _root timeline and there is no local variable called 'a'.

This is usually a subtle difference, but can be very important. One should
understand how these things work so you don't get bitten by a strange 'bug'
one day :)
--
All the best,
Jeckyl

AddThis Social Bookmark Button