2 Replies Latest reply on Feb 27, 2013 2:47 PM by mixedherbs

    define variables in

    mixedherbs

      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?

        • 1. Re: define variables in
          Nabren Level 3

          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

          1 person found this helpful
          • 2. Re: define variables in
            mixedherbs Level 1

            Thank you Nabren, success!!!