5 Replies Latest reply on Oct 1, 2012 1:49 PM by kglad

    Check text in string AS3.0

    gabessdsp Level 1

      So for my Flash class in school I'm making a game, this game has a keypad in it that if you enter the right code it unlocks the door. I have most of it down but am still confused.

       

      So far I have a dynamic text box and when you hit the number buttons I'm using appendText() to add the number to the textbox. There's a clear button that clears the box, and an enter button to enter the code.

       

      buttonEnter.addEventListener(MouseEvent.CLICK,codeCheck);

       

       

      function codeCheck(e:MouseEvent):void {

                if (KEYPAD_TEXT.text == num1 + num2 + num3 + num4){

                          keypadUnlock = true;

                          trace(keypadUnlock);

                }

                else {

                          KEYPAD_TEXT.text = String("Invalid");

                          trace(keypadUnlock);

                }

      }

       

       

      Above is the code I am currently using, if I click the enter button right now all it does is tell me the code is invalid wether the code is right or not. This is because I don't know how to check what is actually in the textbox. So if the correct code is 1234 and I enter 1234, how do I make the if statement check wether or not the string says 1234 so that I can set my keypadUnlock to true and unlock the door?

       

       

      If you need any more information please ask.

        • 1. Re: Check text in string AS3.0
          kglad Adobe Community Professional & MVP

          use:

           

           

          buttonEnter.addEventListener(MouseEvent.CLICK,codeCheck);

           

           

          function codeCheck(e:MouseEvent):void {

                    if (KEYPAD_TEXT.text == "1234"){

                              keypadUnlock = true;

                              trace(keypadUnlock);

                    }

                    else {

                              KEYPAD_TEXT.text = String("Invalid");

                              trace(keypadUnlock);

                    }

          }

           

          1 person found this helpful
          • 2. Re: Check text in string AS3.0
            gabessdsp Level 1

            I thought of that but here's something I forgot to add that makes that not work. I don't have a definite set of numbers that unlocks the keypad. Everytime you start a new game I have this code:

             

            var num1:int = Math.round(Math.random()*8);

            var num2:int = Math.round(Math.random()*8);

            var num3:int = Math.round(Math.random()*6);

            var num4:int = Math.round(Math.random()*3);

             

             

            and some more to make sure none of the numbers are the same. But for example if the first time you play the code is 1234 then you play again and try 1234 the  code will be wrong because it might be 4321. This is a valuable bit of information I forgot to include

            • 3. Re: Check text in string AS3.0
              kglad Adobe Community Professional & MVP

              use:

               

               

              kglad wrote:

               

              use:

               

               

              buttonEnter.addEventListener(MouseEvent.CLICK,codeCheck);

               

               

              function codeCheck(e:MouseEvent):void {

                        if (KEYPAD_TEXT.text == String(num1)+String(num2)+String(num3)+String(num4)){

                                  keypadUnlock = true;

                                  trace(keypadUnlock);

                        }

                        else {

                                  KEYPAD_TEXT.text = String("Invalid");

                                  trace(keypadUnlock);

                        }

              }

               

               

              p.s.  please mark helpful/correct responses.

              • 4. Re: Check text in string AS3.0
                gabessdsp Level 1

                I usually wait until I get the correct answer to mark helpful ones since I don;t know when it will be answerd and one post may be more helpful than the other. But thank you so very much! This is a simple solution I can't believe I overlooked....

                • 5. Re: Check text in string AS3.0
                  kglad Adobe Community Professional & MVP

                  you're welcome.