5 Replies Latest reply on Sep 4, 2009 9:30 PM by TheFlexGuy

    TextInput and Integers

      Hi, I've spent the past hour or two trying to figure something out.
      I have to textinputs, one called "Adults" and one called "Children". The point of the flash file is to get the number of adults and children. I thought it would be so easy :)

      I thought: var total1=(adults*6)+(children*3); would work, but it didn't.
      Then I experimented with Number() and parseInt() and int() and I still can't get anywhere.

      I just want to get the numbers from both textinputs then change a Dynamic Text box to total1.

      Could somebody help me please? :)
        • 1. Re: TextInput and Integers
          Rothrock Level 5
          Well as I think you guessed, TEXT fields have TEXT values. Crazy huh? :)

          You say you've experimented with parseInt() and aren't getting anywhere. That is odd, because that is the answer. (Assuming you want an integer!) My guess is that you aren't getting the value of the textField. Remember that to get the contents of a text field, as opposed to some other property of the field you need to do this.

          trace("The value of the Adults field is "+Adults.text);

          But that is text (a string, not a number) even though it looks like a number. To get the number.


          That should work.
          • 2. Re: TextInput and Integers
            A584537 Level 1

            My trace isn't working. When I run this in Debug mode, nothing prints out in Output (F2). Also, when I do: total=total1; It is always NaN (not a number) even though I use var total1:Number and parseInt(adults.text);

            var total1:Number=parseInt(adults.text)+parseInt(children.text);

            Total is the dynamic text box.
            • 3. Re: TextInput and Integers
              Rothrock Level 5
              Do this.


              If that doesn't come back with something that at least looks like a number then the problem is something along the lines of there is no text field called Adults, the text field isn't in the timeline you were thinking, the value hasn't been input yet, or something else. Because if there is a text field called Adults and it has a number in it, the code you have shown will work.
              • 4. TextInput and Integers
                A584537 Level 1
                I have done this:


                And nothing is output(ted?). The two text boxes are all lowercase so I didn't make a mistake in the names when I did something like this. So it should work. On the input boxes children and adults their Var's are named respectively and I just changed the Instance name to the same and that still didn't work.
                I'm pretty new to this (as you can probably tell :) ) so I might be overlooking something pretty silly.
                Everything is in Slide1 and all the code and input boxes and the button are in the same frame, frame 3.

                I just went back to frame 1 to check if trace works there and it doesn't. I have 4 input boxes in Frame 1, one is called name; so I did trace(name); and trace(name.text) and nothing (text was in the box).
                trace("hello"); didn't work either.
                It seems I have a new problem :(

                Edit: If I drag out a TextInput box and set the var attribute in the properties box to adults. Why can't I use adults.text?
                If I set the instance name to adults, then the box just has _level0.adults in it and I have to delete it before I enter anything.

                Edit2: If I do total=adults.text; (total being a dynamic text box, and I can't set total.text because it doesn't show anything on screen if I change it) anyway, if I do that, the dynamic text box always shows "undefined".
                • 5. Re: TextInput and Integers
                  TheFlexGuy Level 2

                  If you restrict the input to just numbers, you can just do this:

                  var numChildren:int = int(children.text);

                  var numAdults:int = int(adults.text);


                  Simple enough, it just might work!


                  Did that solve it?