5 Replies Latest reply on May 3, 2010 7:41 AM by riquigley

    Extracting numbers, not strings from input text fields?

    celebritymusic Level 1

      Hi

       

      I have two input boxes that I can type numbers into.  I'd like a third input box to display the total of the first two.

       

      I've set up a key pressed listener on the stage to run a function that adds them together when a key is pressed.

       

      Problem is, it just places the strings together - it ddoesn't add them

       

      So how do I tell Flash that box1.text and box2.text are numbers?

       

      box3.text = box1.text + box2.text ain't doing it.

       

      Thanks for your time.

       

      Shaun

        • 1. Re: Extracting numbers, not strings from input text fields?
          riquigley

          Hi celebrity Music

           

          You will need to cast the String as a Number.  Something like:

           

          var firstNumber:Number = Number(box3.text);

          var secondNumber:Number = Number(box2.text);

           

          var answer:Number = firstNumber + secondNumber;

           

          Of course, you'll want to make sure that the text in the text field is actually a number and someone wasn't able to type in a word.  The best way to do this is to NOT make the textfield a text input field.  Instead make it a dynamic text field, and give it an MouseEvent.CLICK event listener.  When someone clicks on the dynamic text input field, bring up a keypad that forces them to enter the number that way.

           

          I just made an app exactly like this where students had to input a number into a text field to figure out econ questions.  The workflow went something like:

           

          1. Click on dynamic text field
          2. Bring up number entry pad
          3. When the user clicks a number on the pad, figure out the cooresponding text using a switch statement
          4. Add the text-numbers into a string field, that way someone who enters "1" "3" "8" will get 138, and not 12.
          5. With each key press, update the dynamic text field to show what the user is entering
          6. With the user presses the Enter key, then cast that text field as a Number and store it in a variable of type Number

           

           

          Hope that helps.  Let me know if you need any help with the code for any of those steps.

          • 2. Re: Extracting numbers, not strings from input text fields?
            celebritymusic Level 1

            Hey Riquigley

             

            Thanks for that tip.

             

            It all makes sense apart from how to set up a number entry pad.  Is that an as3 component you buy from somewhere?

             

            Thanks for your help - I appreciate it.

             

            Shaun

            • 3. Re: Extracting numbers, not strings from input text fields?
              celebritymusic Level 1

              On second thought, just so I understood how it worked, I tried doing it the first way you mentioned:

               

                  var r101var:Number = Number(r101.text);
                  var r201var:Number = Number(r201.text);
                  var tot01var:Number = r101var + r201var;
                  trace (tot01var);
                  tot01.text = tot01var;

               

              The trace outputs the correct answer eg if r101 is 7 and r201 is 10, it outputs 17.

               

              However, the final line generates an error: 1067: Implicit coercion of a value of type Number to an unrelated type String.

               

              Why would that be?

              • 4. Re: Extracting numbers, not strings from input text fields?
                riquigley Level 2

                On the last line you need to cast the number back into a String for it to work in the textfield.

                 

                    tot01.text = String(tot01var);

                 

                Anytime your trying to convert one type of variable into another, you have to "cast" the variable into something else.  The process for it is always the same, just tell ActionScript what you want the variable to be as in this format:

                 

                NewVariable = NewVariableType(OldVariable)  as in

                 

                var firstVariable:String = "20";

                var secondVariable:int;

                secondVariable = int(firstVariable);

                 

                If AS3 isn't able to convert from one to another because the two types are not compatible, you'll get an error.

                 

                Also, sometimes ActionScript doesn't know if what you're trying to do to a variable CAN be done, because you're not explicit enough about what kind of variable it is.  For example, if you make a reference to a MovieClip's parent MovieClip:

                 

                parent.sampleMovieClip

                 

                actionScript often worries that the parent may not also be a MovieClip, so you have to "cast" it to re-assure ActionScript that it will all be okay:

                 

                MovieClip(parent).thisImage

                 

                To read more about type conversions, visit this page.

                 

                Concerning the number entry pad, it is something you'll have to create.  If I have time later,  I can post one that I made a few months ago for the econ project I did.  You can see it in action here:

                 

                http://www.gtcc.edu/distance/learning_objects/flash/econ/CircularChart.html

                 

                It's just a matter of going in and separating out the number pad for you.

                1 person found this helpful
                • 5. Re: Extracting numbers, not strings from input text fields?
                  riquigley Level 2

                  The FLA file containing the MovieClip:

                  http://www.gtcc.edu/distance/learning_objects/flash/test/numInput/numberInput.fla

                   

                  The AS file that controls the Number Input MovieClip

                  http://www.gtcc.edu/distance/learning_objects/flash/test/numInput/NumInput.as

                   

                  The custom Event that the Number Input AS file references for sending off the final number:

                  http://www.gtcc.edu/distance/learning_objects/flash/test/numInput/NumEvent.as

                   

                   

                  Just right-click and SAVE AS to download the as files

                   

                  Of course, the as files are packaged using my own directory structure, so just pasting it into what you're doing won't work, but hopefully it will help give you some ideas about what to do.

                  1 person found this helpful