A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Converting input text to an integer

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Converting input text to an integer

    I apologize if the answer is somewhere on the forum, but it is late it I have searched and tried many things. I am new to flash, but not to programming, and I am having problem doing something. I am in the process of creating an education site for my wife, and am planning on using flash to create learning game for young kid to use. As I am just learning, I am starting slow, and trying to crea a simple addition game in flash. right know it create two random numbers between 1 an 5 and adds the. What I am having problem figuring out is how to get a number input form a input. I have a input text area, but I can not conver the value input to a number. I have searched the internet, and have tried int, parseInt, Number, and maybe a couple other, to no avail. I am using action script 2.0. The result I get is NaN when I try to display the number I converted from the input. Any help would be greatly appreciated. Thanks, Troy

  2. #2

  3. #3
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    No Datatyping. This is why as2 and javascript kinda suck.
    Try converting everything step by step.
    Code:
    myString="3";
    myNumber=Number(myString);
    myNumber+=1;
    trace(myNumber);
    
    // Output is 4
    [SIGPIC][/SIGPIC]

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Thanks for the responces

    rachelg: I visited the website, was not able to get that solution to work either.

    rynow: I tried you suggestion also, still not luck.

    The code I am using is listed here:

    Code:
    on (release) {
    	var answer = num1 + num2;
    	test1 = answer;
    	test2 = Number(answerNumber.text);
    	if (answer == int(answerNumber)) {
    		answerResult = "Correct";
    	} else {
    		answerResult = "Incorrect";
    	}
    
    }
    This code is attached to a button. It first finds the answer. The test1 and test2 are test fields I added to help debug. I have tried int(answerNumber), parseInt(answerNumber), and Number(answerNumber). As listed I get a "NaN" in the test2 box. I have not changed the conditional at all, as I an trying to solve the conversion thing first. Again, thanks for the help. If you need further information, I will be happy to provide anything you need. Troy

  5. #5
    Member
    Join Date
    Aug 2012
    Posts
    55
    are num1 and num2 the names of the textboxes?

    in which case try this

    on (release) {
    var answer:Number = parseInt(num1) + parseInt(num2);
    test1 = answer;
    test2 = parseInt(answerNumber.text);
    if (test1 == parseInt(answerNumber)) {
    answerResult.text = "Correct";
    } else {
    answerResult.text = "Incorrect";
    }

    }
    Last edited by rachelg; 09-06-2012 at 12:50 PM.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    No, they are random number I use for the problem. They range between 1 and 5.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    I made the changes you suggested. Still getting "NaN" in test2.

    Could this be because I am using Action Script 2.0 instad of 3.0. I am using 2.0 because Flash would not let me attach code the the button in 3.0 or ginve the text area a variable name. I there is a way around thes issues, I will swithc to Actionscript 3.0.

    Thanks again for yuou help. Troy

  8. #8
    Member
    Join Date
    Aug 2012
    Posts
    55
    are they held on the stage then? they should be referenced with _root.num1 etc then

    what's the input box called that holds them? make sure they have instance names and var names assigned

  9. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Would it help if I sent the flas file to you? If so, send me you email to troylmarker@gmail.com, and I will send the Falsh soure file.

  10. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    Just figured it out. It was the instance names. Previousely I was not using them, as I did not fully understand the concept. Once I assigned the answer field and instand name, and used that in the code, it worked. Thanks rachelg for all you help. This learning a new programming language cna be a chanllenge at time, but it is rewarding.

  11. #11
    Member
    Join Date
    Aug 2012
    Posts
    55
    great! so glad you got it to work

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center