8 Replies Latest reply on May 18, 2011 2:40 PM by Ned Murphy

    Input Text results in if-then

    kirrwed Level 1

      Hopefully this is very simple but I have yet to find a solution in AS3 out there.

       

      I want to have an input text box with the instance name "answer1_input."

      If the user types in "one" I want it to go to frame 2.

      If the user types in anything else, I want it to go to frame 3.

       

      What is the code?

        • 1. Re: Input Text results in if-then
          SirDragoon Level 1

          Here a small exemple, you just need to set Main.as Class to your FLA file.
          You can write one and press ENTER.


          package {
             
              /**
               * ...
               * @author Michaël Chartrand
               */
             
               //IMPORTATIONS
              //--------------------------------------------------------
              import flash.events.Event;
              import flash.events.MouseEvent;
              import flash.text.TextField;
              import flash.text.TextFieldType;
              import flash.display.MovieClip;
              import flash.events.KeyboardEvent;
             
              //CLASS Main
              //--------------------------------------------------------
              public class Main extends MovieClip {

           

                  //PROPRIÉTÉS
                  //--------------------------------------------------------
                  public var answer1_input:TextField;
             
                  //FONCTION Main
                  //--------------------------------------------------------
                  function Main() {
                      this.addEventListener(Event.ADDED_TO_STAGE, init);
                  }
                 
                  //FONCTION init
                  //--------------------------------------------------------
                  function init(e:Event):void {
                      this.removeEventListener(Event.ADDED_TO_STAGE, init);
                     
                      //create input
                      answer1_input = new TextField();
                      answer1_input.x = 25;
                      answer1_input.y = 25;
                      answer1_input.selectable = true;
                      answer1_input.width = 200;
                      answer1_input.height = 25;
                      answer1_input.border = true;
                      answer1_input.type = TextFieldType.INPUT;
                      this.stage.addEventListener(KeyboardEvent.KEY_UP, isWritingEvent);
                      addChild(answer1_input);           
                  }
                 
                  //FONCTION isWritingEvent
                  //--------------------------------------------------------
                  function isWritingEvent(e:KeyboardEvent):void {
                      trace(e.keyCode);
                      if(e.keyCode == 13){ //13 = ENTER
                          if (answer1_input.text == "one") {
                              this.gotoAndStop(2);
                          }else {
                              this.gotoAndStop(3);
                          }
                      }
                  }
                 
              }
          }

          • 2. Re: Input Text results in if-then
            kirrwed Level 1

            Thank you, but that is not what I'm looking for.  I am looking for a simple if-then function assigned to an input text field.

            • 3. Re: Input Text results in if-then
              Ned Murphy Adobe Community Professional & MVP

              What type of controls do you intend to apply to it.  If the user is starting to type "one", they first type an "o"... what controls do you want in place to prevent making an early break for frame 3?  Will they click a button after they are done typing?

              • 4. Re: Input Text results in if-then
                kirrwed Level 1

                How about I want it only to submit the text when they hit enter.

                Or if I need to add a "submit" button, I am flexible. 

                • 5. Re: Input Text results in if-then
                  Ned Murphy Adobe Community Professional & MVP

                  You pretty much already got that in what the first response provided, but here it is in non-class format where the textfield is manually added to the stage...

                   

                  stage.addEventListener(KeyboardEvent.KEY_UP, isWritingEvent);


                  function isWritingEvent(e:KeyboardEvent):void {
                      if(e.keyCode == 13){

                           if (answer1_input.length > 0 && answer1_input.text == "one") {
                                 gotoAndStop(2);
                           } else {
                                 gotoAndStop(3);
                           }
                      }
                  }

                  • 6. Re: Input Text results in if-then
                    kirrwed Level 1

                    Ned, that worked, thanks.  What is does the line with the e. and 13 do?

                    Also, can you make it not-case sensitive?  Right now, I tested it by typing in ONE and it didn't work.  But "one" works ok.

                    • 7. Re: Input Text results in if-then
                      Ned Murphy Adobe Community Professional & MVP

                      The "e" is the event argument that gets sent by the event listener when it calls on the event handler function.    That argument holds information that can be used such as in this case, to identify which keyCode of the key was just released.  The keyCode for the ENTER key is 13.

                       

                      stage.addEventListener(KeyboardEvent.KEY_UP, isWritingEvent);


                      function isWritingEvent(e:KeyboardEvent):void {
                          if(e.keyCode == 13){

                               if (answer1_input.length > 0 && answer1_input.text.toLowerCase() == "one") {
                                     gotoAndStop(2);
                               } else {
                                     gotoAndStop(3);
                               }
                          }
                      }

                      1 person found this helpful
                      • 8. Re: Input Text results in if-then
                        Ned Murphy Adobe Community Professional & MVP

                        I should have added that the "e" is just a variable name assigned to the argument that is received by the function.  It could be named anything, much the same way you can name the textfield as you please.  Some people get hung up thinking it is something more meaningful than what it really is.