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

flash actionscript : Referring to an Instance


Dan-C
11/14/2004 6:28:07 PM
Hello. Ok, I hope I can explain this properly. I want to create a function
that dynamically refers to different instances based on a variable, and I know
this can be done, but I don't know the syntax. Here's an example: function
myFunction(myNumber){ myLoader2.load(); } I want to replace myLoader2 with
myLoader[myNumber] so that I can use the myNumber variable to refer to the
specific loader instance. Can anyone help me out with this? Thanks a lot.
Blade alSlayer
11/14/2004 8:33:44 PM
Here is how it goes:
var myVar:Number = 3; // just an example value;
// The instance name should be:
["myLoader" + myVar].load();
// before the opening [ you may specify a path. For example:
_root["myLoader" + myVar].load();
// NOTE: there is NO dot (.) between the _root keyword (or other path) and the
opening [!!!
Blade alSlayer
11/14/2004 8:34:45 PM
Dan-C
11/14/2004 8:40:01 PM
Ok, I had tried that before, and I tried it again now and it doesn't seem to
work for me. I get a compile error. It doesn't seem to like the brackets. I
am working in Flash 7 saving as Flash 6 actionscript version 2. I'm not sure
that any of that matters. Here is the compile error I get. Any other
suggestions? **Error** Symbol=contentPhotos, layer=Functions, frame=1:Line 15:
Unexpected '.' encountered ['loader'+myVar].load(); **Error**
Symbol=contentPhotos, layer=Functions, frame=1:Line 17: Unexpected '}'
encountered } Total ActionScript Errors: 2 Reported Errors: 2
Dan-C
11/14/2004 8:46:21 PM
Ok, in playing around with this I found out what it needs. The code you had
was correct, except that it needs the '_root' or something in front of it. So
where this: ['loader'+myVar].load(); Wouldn't work, this:
this['loader'+myVar].load(); Does work. Thanks a lot.
AddThis Social Bookmark Button