3 Replies Latest reply on May 19, 2010 5:23 AM by Ned Murphy

    String and Integer issue

    sjcmagic

      I'm creating a button that when clicked loads a corresponding text field into an existing text field. Here's the code:

       

      myVariable1 = "my text";

      myVariable2 = "more text";

       

      for (i=0; i<=8; i++) {


          this["circle"+i].uniqueID = i;


          this["circle"+i].onRelease = function() {
             
              test_str = myVariable+this.uniqueID

       

             
          };
      }

       

      So everytime the circle1 button is clicked myVariable1 should load into the test_str dynamic text field. When circle2 is clicked myVariable2 should load and so on. The problem is I keep getting a not a number (NaN) response in the text field. I know it has something to do with combining a string with a number but can't solve this. I've tried toString() and other bits of code but it's not working. Thanks in advance.

        • 1. Re: String and Integer issue
          Ned Murphy Adobe Community Professional & MVP

          You need to use the same approach...

           

          test_str = this["myVariable"+String(this.uniqueID)];

           

          And if test_str is a var assigned to the textfield, it is not a reliable practice to use textfield vars.  You should assign an instance name to the textfield and assign text to it using the text property, as in .... textFieldName.text = "etc....";  where textFieldName is whatever instance name you assign

          • 2. Re: String and Integer issue
            sjcmagic Level 1

            This is coming back as undefined with the changed code:

             

            myTextField.text = "testing"

             

            myVariable1 = "working"

             

            for (i=0; i<=8; i++) {

             

                this["circle"+i].uniqueID = i;

             

                this["circle"+i].onRelease = function() {

             

                    myTextField.text = this["myVariable"+String(this.uniqueID)]

             


             

                };

             

            }

             

            The field starts as "testing" but when I click the circle1 button it changes to undefined instead of "working."

            • 3. Re: String and Integer issue
              Ned Murphy Adobe Community Professional & MVP

              Three things:

               

              1) There is a minor tangle of "this" in the onRelease function.  Inside that function, "this" refers to the circle object, but your variable lies outside in the main timeline, so....

               

              myTextField.text = _root["myVariable"+String(this.uniqueID)];

               

              2) Your loop starts at 0, but your variables start at 1.  Be sure to make them agree, otherwise your code is looking for myVariable0 which won't be found

               

              3) When you declare a variable, declare a variable...

               

              myVariable1 = "working"

               

              should be...

               

              var myVariable1 = "working";