PDA

View Full Version : really simple question...


jlackyashdya
09-12-2007, 12:12 PM
hi all,
i m developing one score board. I m using Input Text Fields for uer input.
Then i want to grab that values and make addition of those values.

I have one "+" sign button on stage. I have assign following code to it.
----------------------------------------------------------------------
on(press)
{
var total = 0;
var a = inputA1.text;
var b = inputA2.text;
var c = inputA3.text;
total = a + b;
trace(total);

}
----------------------------------------------------------------------
but its not doing addition.
its just showing me values of var a and var b entered by the user.

eg. if user enters "10" in inputA1 and "20" in inputA2. Then it should show me addition of "10 + 20" ie "30".
but its showing me "10" "20".

can anyone help me???

waiting for ur help

panel
09-12-2007, 03:31 PM
you have to convert strings to numbers


on(press)
{
var total = 0;
var a = Number(inputA1.text);
var b = Number(inputA2.text);
var c = Number(inputA3.text);
total = a + b;
trace(total);

}

Noct
09-12-2007, 03:42 PM
Anything entered into a text field is handled as a string value.
What you are doing above is adding two string values together, which just creates one long string.

Like:
var strVar1:String = "x";
var strVar2:String = "y";
trace(strVar1+strVar2);
//returns "xy"

You must convert the values in your inputTexts to numbers before adding them together:
Use parseInt() to convert a string to an integer, or parseFloat() for decimal values:
var total = 0;
var a = parseInt(inputA1.text);
var b = parseInt(inputA2.text);
total = a+b;
trace(total);

......
Well, panel beat me to the post, but I would suggest using parsing over calling Number.
Number will not always give the results you need.

jlackyashdya
09-13-2007, 06:57 AM
Thanks "panel" and "Noct"!!!

Noct, ur code is working!! u r right! we need to parse the integers.


Thanks a lot!!:)