Copy link to clipboard
Copied
Hi guys,
A really simple question probably 🙂
I'm using Flash MX 2004 Pro , AS2
There is a name variable which will be taken from an input text field.
Trying to use "for" to take the lenght of the name and store every
letter in a variable. All this within the "for".
letter0=J
letter1=o
letter2=n
etc.
the script:
thename = "Jonathan";
for (i=0; i<thename.length; i++) {
"letter"+i = thename.charAt(i);
}
Gives me an error at compiling:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Identifier expected
var "letter"+i = thename.charAt(i);
Total ActionScript Errors: 1 Reported Errors: 1
How could I do this?
Copy link to clipboard
Copied
You can use an object:
var letterObject:Object = new Object();
thename = "Jonathan";
for (i=0; i<thename.length; i++) {
letterObject["letter"+i] = thename.charAt(i);
}
You can then access it later by letterObject.letter0 or letterObject["letter0"] (the latter is better for iterations because you can't go letterObject.letter<variableName> expecting it to replace the number for you)
EDIT:
You can also use the this object:
thename = "Jonathan";
for (i=0; i<thename.length; i++) {
this["letter"+i] = thename.charAt(i);
}
then you would just access by this.letter0 or this["letter0"] or just by plain old letter0
Copy link to clipboard
Copied
Thank you Nabren, success!!!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now