PDA

View Full Version : difference between . access and [] access



vcorn4
January 7th, 2004, 01:17 AM
hi all
i refer to this tutorial http://www.kirupa.com/developer/mx/mousetrailer.htm
what i found strange is that i can't use . to refer to movie clip instance in this tutorial, it only works if i use [] access
for example:
"_root.instancename" versus "_root[instancename]"
only the second one works....
please examine the short code i attached just cut and paste the code...play the movie..and see the difference,
anybody can explain why? or i did any mistake?
thx for help

attachment below

ahmed
January 7th, 2004, 01:28 AM
say you have 5 clips on _root, mc1, mc2.. mc5. Accessing them with the dot-notation would like this:

_root.mc1
_root.mc2
_root.mc3
_root.mc4
_root.mc5

Using the _root associative array, you would access them like this:

_root["mc1"];
_root["mc2"];
_root["mc3"];
_root["mc4"];
_root["mc5"];


Now, say you want to iterate through the clips with a for-loop, you can't go like

for ( var i=1; i<6; i++ )
trace( _root.mc+i );

That simply wouldn't work, so what you'd do is:

for ( var i=1; i<6; i++ )
trace( _root["mc"+i] );

I hope that helps at all.. There's a thread lying around somewhere by Senocular on associative arrays, you might wanna have a look at that too :)

vcorn4
January 7th, 2004, 01:50 AM
hmm..thx alot
so there is noway that we can use . access in a for loop ?
so _root.mc1 , --> the mc1 is not string...is that what you mean?

vcorn4
January 7th, 2004, 01:51 AM
can i do like,
mc = "mc1" where mc1 is the instancename of movie clip
then i access like _root.mc
will it be successful?

ahmed
January 7th, 2004, 02:47 AM
Nope, _root.mc would refer to the variable 'mc' not the value assigned to it:)

norie
January 7th, 2004, 03:10 AM
if you were to trace _root.mc, it would return "mc1" as a string not an object. Think of the value in the array as the begining object itself (basically what it is):


_root["myclip"]
//root is the array that contains the valid reference
//to the object _root.myclip. so any properties set, will be
//applied to the specified object inside that array.



mc = "mc1";
_root.mc;
//you are referencing a valid object, but the object
//is a variable in this case. So any properties being applied
//will be applied to the variable itself.

another alternative (although i don't see it used much):


mc = eval("mc1");
_root.mc;
//eval evaluates the a variable or string to refer to an object.

vcorn4
January 7th, 2004, 03:46 AM
Thank you guys,
understand now:)