2 Replies Latest reply on Apr 20, 2009 6:47 PM by RossRitchey

    Checking number variables against input text

    FlashTapper Level 1

      Am designing an educational activity whereby users must answer some mathematical questions regarding decimal places. Answers are generated as a list of variables when the movie starts. Then what the user types into the text field is converted into a variable as well and compared to the exisitng answer variables that were already previously generated. If the user gets the answer right, the textbox background colour turns green, if it is wrong it turns red.

       

      Ahhh...all good in theory, it nearly works in fact - but certain questions just don't seem to be listening or obeying my code. It seems to happen on answers that have several decimal places before the zero (e.g. 0.00782). I am using Number variables as I don't think there is a decimal variable type. Problems exist on answers 5, 9 & 11.

       

      decimals.jpg

       

      But this theory goes out the window when I test the last column of answers and find they are all marked correctly and they have several decimal places before the zero. I have also checked that the text input fields are single line and not multiline - been there before!

       

      Have checked and checked and re-checked this code and I really cannot find out what I am doing wrong.

       

      Attached is my Flash CS3 coding. (it's not the cleanest yeah, but it should work (and i'm too lazy to construct a loop).

       

      Any help would be appreciated before I lose a large quantitiy of hair today.

       

      var counter:Number = 0;
      reset_btn._visible = 0;
      resultBox._visible = 0;
      
      
      
      var answer1:Number = 4.7;
      var answer2:Number = 38;
      var answer3:Number = 760;
      var answer4:Number = 0.62;
      var answer5:Number = 0.018;
      var answer6:Number = 9700;
      var answer7:Number = 6.2;
      var answer8:Number = 0.81;
      var answer9:Number = 0.00325;
      var answer10:Number = 63000;
      var answer11:Number = 0.00782;
      var answer12:Number = 23.4;
      var answer13:Number = 978;
      var answer14:Number = 0.032;
      var answer15:Number = 0.00046;
      var answer16:Number = 0.0097;
      var answer17:Number = 0.087;
      var answer18:Number = 341;
      
      
      
      submit_btn.onRelease = function() {
       var userText1:Number = ans1.text;
       var userText2:Number = ans2.text;
       var userText3:Number = ans3.text;
       var userText4:Number = ans4.text;
       var userText5:Number = ans5.text;
       var userText6:Number = ans6.text;
       var userText7:Number = ans7.text;
       var userText8:Number = ans8.text;
       var userText9:Number = ans9.text;
       var userText10:Number = ans10.text;
       var userText11:Number = ans11.text;
       var userText12:Number = ans12.text;
       var userText13:Number = ans13.text;
       var userText14:Number = ans14.text;
       var userText15:Number = ans15.text;
       var userText16:Number = ans16.text;
       var userText17:Number = ans17.text;
       var userText18:Number = ans18.text;
      
       if (userText1 == answer1) {
        ans1.backgroundColor = 0xDBF9E7;
        trace("correct!");
        counter++;
       } else {
        ans1.backgroundColor = 0xFFD9D9;
       }
       if (userText2 == answer2) {
        ans2.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans2.backgroundColor = 0xFFD9D9;
       }
       if (userText3 == answer3) {
        ans3.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans3.backgroundColor = 0xFFD9D9;
       }
       if (userText4 == answer4) {
        ans4.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans4.backgroundColor = 0xFFD9D9;
       }
       if (userText5 == answer5) {
        ans5.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans5.backgroundColor = 0xFFD9D9;
       }
       if (userText6 == answer6) {
        ans6.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans6.backgroundColor = 0xFFD9D9;
       }
       if (userText7 == answer7) {
        ans7.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans7.backgroundColor = 0xFFD9D9;
       }
       if (userText8 == answer8) {
        ans8.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans8.backgroundColor = 0xFFD9D9;
       }
       if (userText9 == answer9) {
        ans9.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans9.backgroundColor = 0xFFD9D9;
       }
       if (userText10 == answer10) {
        ans10.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans10.backgroundColor = 0xFFD9D9;
       }
       if (userText11 == answer11) {
        ans11.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans11.backgroundColor = 0xFFD9D9;
       }
       if (userText12 == answer12) {
        ans12.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans12.backgroundColor = 0xFFD9D9;
       }
       if (userText13 == answer13) {
        ans13.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans13.backgroundColor = 0xFFD9D9;
       }
       if (userText14 == answer14) {
        ans14.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans14.backgroundColor = 0xFFD9D9;
       }
       if (userText15 == answer15) {
        ans15.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans15.backgroundColor = 0xFFD9D9;
       }
       if (userText16 == answer16) {
        ans16.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans16.backgroundColor = 0xFFD9D9;
       }
       if (userText17 == answer17) {
        ans17.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans17.backgroundColor = 0xFFD9D9;
       }
       if (userText18 == answer18) {
        ans18.backgroundColor = 0xDBF9E7;
        counter++;
       } else {
        ans18.backgroundColor = 0xFFD9D9;
       }
       //submit_btn._visible = 0;
       reset_btn._visible = 1;
       resultBox._visible = 1;
       //trace("you have got" + counter + "correct out of 18");
       resultBox.resultNumber.text = Number(counter);
      };
      

       

       

       

        • 1. Re: Checking number variables against input text
          あだ名

          In the future, I would definetely start using arrays instead invididual variables for each item. Also the dictionary object was designed perfectly for this type of situation. Regarding the number problem; You are trying to convert a text string to a number, dont you get this error when you try to compile your code?

          "1067: Implicit coercion of a value of type String to an unrelated type Number".

          you need to convert the user input to a parse float:

           

          var userText12:Number = parseFloat(ans12.text);

          • 2. Re: Checking number variables against input text
            RossRitchey Level 4

            This may sound odd, but have you tried taking those that don't work and converting them to String comparisons?

             

            Convert the answer var to a string, and compare it directly to the .text of the input text field.

             

            Humorous aside: I thought constructing the loops was what one does when lazy.......

             

            Otherwise, the code looks accurate.  So check, double check, and triple check that all of the text fields have the appropriate instance names.

             

            And, to test further, trace out what is in them on submit ( trace(ans5.text); ) to make sure that what is in there is what you are expecting.  Perhaps the "display as HTML" accidentally got toggled for those 3 text fields and there is extra text.  You never know for sure until you throw some traces in there.