5 Replies Latest reply on Oct 27, 2009 10:56 AM by Ned Murphy

    Add two numbers without using variables?

    Andymc7 Level 1

      Hi, I have yet again another simple problem that I've tried to solve 10 different ways, and nothing works. I'm simply trying to add two input text fields' numbers together when the user presses the left arrow key on the keyboard. I'm trying to avoid just typing names in the Var boxes since the text fields' formatting are slightly different. Here's my current code I have attached to the button:

       

      on (keyPress "<Left>") {
          var WagerA = int(_root.AWager.text);
          var ScoreA = int(_root.Ascore.text);
          WagerA+ScoreA;

      }

        • 1. Re: Add two numbers without using variables?
          Ned Murphy Adobe Community Professional & MVP

          As far as I know there is no "int" data type in AS2, so you probably need to cast the text as "Number" types.  Also, your last line of code will do nothing as you need to do something with that sum, such as assign it somewhere...

           

          on (keyPress "<Left>") {
              var WagerA = Number(AWager.text);
              var ScoreA = Number(Ascore.text);
             trace(WagerA+ScoreA);
          }

          1 person found this helpful
          • 2. Re: Add two numbers without using variables?
            Andymc7 Level 1

            Ok Ned, thanks for the quick reply. What I'm trying to do is add the number from the AWager textfield to the number in the Ascore textfield upon pressing the left arrow key. I've tried this also:

            on (keyPress "<Left>") {
                var WagerA = Number(AWager.text);
                var ScoreA = Number(Ascore.text);
                AScore.text = (WagerA+ScoreA);
            }

             

            ...and it doesn't work either.

            • 3. Re: Add two numbers without using variables?
              Ned Murphy Adobe Community Professional & MVP

              If you want to use the keyboard, why are you using button code?  That code will work for the keyboard if it is attached to a button.  When you say it doesn't work, are you  getting any error messages?

              • 4. Re: Add two numbers without using variables?
                Andymc7 Level 1

                The reason I'm using button code is because I want to be able to press the left arrow key once to alter the value of my text field. "key.isDown" doesn't work as it repeatedly adds to the number as long as the key is pressed down (unless, of course, I can press the button and let off very quickly). I would be happy to use something other than button code if there is an alternative to using (Key.isDown(Key.LEFT)). I hope I'm making some sense. Basically, when I press a key, I would like for it to increase the amount of a number one time, and stop.

                 

                No, I didn't get any error messages at all while trying my code. The left arrow key simply didn't do anything at all.

                • 5. Re: Add two numbers without using variables?
                  Ned Murphy Adobe Community Professional & MVP

                  Try using...

                   

                  var keyListener:Object = new Object();
                  Key.addListener(keyListener);

                   

                  keyListener.onKeyUp = function(){
                        if(Key.getCode() == Key.LEFT){
                              trace("left key"); // use for test

                              // insert your code here
                        }
                  }

                   

                  This code will work for the left key, which the trace will demonstrate.  If it is not processing your textfield data, then the problem is more likely with the textfields