|
-
Dead Quick Question...
This will be really easy for most of you im sure:
I've got a few variables named like this:
Person1name
person2name
person3name
and
name1
name2
name3
How do i call each one using the same loop?
eg.
n=0
while(n < 6){
Person("n")name = "hi";
n=n+1
}
Thanks very much for this, its been driving me crazy!!!
cheers/
-
Senior Member
You can use an array-style notation to refer to variables whose names are constructed dynamically.
Try this:
code:
for (i = 1; i <= 3; ++i)
{
varname = "Person" + i + "name";
varval = this[varname];
trace(varval);
varname = "name" + i;
varval = this[varname];
trace(varval);
}
If this[varname] doesn't work, replace it with _root[varname] (it depends on how your script is setup).
This technique is somewhat cumbersome however. If it were me, I'd store the variables in an array, like so:
code:
people = ["Charley", "Fred", "Wilma"];
for (i = 0; i < people.length; ++i)
{
varval = people[i];
trace(varval);
}
-
That only seems to work for text..
I.e I can use arrays as peoples names,
But it wont move a movieclip using arrays.
e.g
MClips = ("clip1", "clip2", "clip3");
_root.MClips[2]._y = 300;
please help.
Thanks.
-
Senior Member
It's only working for strings because of the quotes. If they're movieclips, remove the quotes.
Also, when specifying arrays like this, use square brackets, not parentheses.
code:
MClips = [clip1, clip2, clip3];
_root.MClips[2]._y = 300;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|