-
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
-
-
Dignitary
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
-
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
-
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.
-
No, they are random number I use for the problem. They range between 1 and 5.
-
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
-
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
-
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.
-
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.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|