4 Replies Latest reply on Mar 25, 2009 4:17 PM by Rothrock

    Variable on Input Text not working with If / Then

    newbie214
      I'm using ActionScript 2.0 with Flash CS4 Professional, and I'm running into conflicting info on how to do this depending on which Flash book I'm looking at (and is that really a surprise?).

      1) I want the user to type text into a text field and then Flash places what they've typed into a variable.

      So I created a text field of the type 'Input Text' and assigned this variable 'myInput'

      2) When the user presses a button, I want Flash to look at what is in the variable. THEN... If they have typed the correct word, they are sent to frame 19. If they type anything else, they are sent to frame 38.

      In this example, the correct word is: "move"

      So, I placed this script on the button:

      on (release) {
      trace(myInput);
      if (myInput = "move") {
      gotoAndPlay(19);
      } else {
      gotoAndPlay(38);
      }
      }

      However, when I run this, and click the button, it jumps to frame 19 regardless of what I enter into the text field.

      So then, I recall seeing in some books where they had a double equals sign, so I tried this:

      on (release) {
      trace(myInput);
      if (myInput=="move") {
      gotoAndPlay(19);
      } else {
      gotoAndPlay(38);
      }
      }

      Now when I click the button, it jumps to frame 38 regardless of what I enter.

      (Also the trace(myInput); shows that the text entered is populating the variable correctly.)

      Any ideas? I'm looking for the shortest, simplest solution possible. We're a training group--not programmers... so we're just looking for the most direct path here. :-) Thanks!
        • 1. Re: Variable on Input Text not working with If / Then
          Ned Murphy Adobe Community Professional & MVP
          The second version you show is the correct version. = is for assigning a value, and == is used for comparing equality. And there is nothing wrong with the code itself, so you need to check the rest of the implementation. Make sure you've designated the variable and not the instance for the textfield.
          • 2. Re: Variable on Input Text not working with If / Then
            Ned Murphy Adobe Community Professional & MVP
            One other thing, if you are using the variable element of a textfield, you should still declare a variable in the timeline for it. This comes in handy when you need to go somehwere in the timeline where that input textfield is no longer being displayed.

            var myInput;
            • 3. Re: Variable on Input Text not working with If / Then
              newbie214 Level 1
              Thanks! I went and added this to the first frame in the timeline and I have it on a separate layer, so that it's available for the entire length of the movie, but it's still not working for some reason.

              Given this, it still keeps jumping to line 38, regardless of what I type into the text field.

              on (release) {
              trace(myInput);
              if (myInput=="move") {
              gotoAndPlay(19);
              } else {
              gotoAndPlay(38);
              }
              }

              I checked the text field and the button and it looks like everything is correct. The variable was assigned to the text field through the Properties window's Variable field. And if I put another text field on another frame, and assign it with the myInput variable, it will display what I typed into that field. So, it knows the value that is in the variable. It just seems like after the trace statement, it is jumping down to:

              gotoAndPlay(38);

              ...and ignoring everything else in between.

              • 4. Re: Variable on Input Text not working with If / Then
                Rothrock Level 5
                Here is a trick, text can contain invisible characters so it might look the same, but still not be the same. In fact, since your code looks right) they must not be the same.

                So trace like this:

                trace("["+myInput+"]");

                See if there is a return or something like that there. If your input text field is multiline then most likely there is.