Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

define variables in

New Here ,
Feb 27, 2013 Feb 27, 2013

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?

TOPICS
ActionScript
450
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 27, 2013 Feb 27, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 27, 2013 Feb 27, 2013
LATEST

Thank you Nabren, success!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines