PD:

Actually, the parseInt part is not needed if you are going to manipulate the
input as text...

So, you can change this line:
resultTxt.text = (parseInt(ageField.text) + inputChar).toString();

to:
resultTxt.text = ageField.text + inputChar;

Since parseInt(ageField.text) is converted to a string anyway before being
concatenated to inputChar.

You should also restrict the input text to accept only numbers or validate
the input by yourself.

Cheers
Juan Pablo Califano

2008/5/17, Juan Pablo Califano <[EMAIL PROTECTED]>:
>
> It seems like the TEXT_INPUT event is fired before the .text property is
> set with the user's input. So, when you try to read the value from the text
> input, it returns an empty string; which, parsed as an integer, returns NaN.
>
> So, you have get the current input char from the event.text field, and
> check if the input text has a lenght > 0.
>
> You can try something like this:
>
> stop();
>
> ageField.addEventListener(TextEvent.TEXT_INPUT, convertAge);
>
> function convertAge(event:TextEvent):void {
>
>  trace("ageField.text.length:"+ageField.text.length);
>
>  var resultTxt:TextField = new TextField();
>  resultTxt.border = true;
>  addChild(resultTxt);
>
>  var inputChar:String = event.text;
>
>  if(ageField.text.length > 0) {
>   resultTxt.text = (parseInt(ageField.text) + inputChar).toString();
>  } else {
>   resultTxt.text = inputChar;
>  }
> }
> Cheers
> Juan Pablo Califano
>
>
> 2008/5/17, Steven Sacks <[EMAIL PROTECTED]>:
>>
>> Your textfield is probably set to multiline or something like that.
>>  Anything like a carriage return will cause parseInt to break.
>>
>> rlyn ben wrote:
>>
>>> need to display number to the resultTxt but when i press the first number
>>> it displays NaN.. when i enter the second number.. it display the number but
>>> with the NaN.. :(
>>> stop();
>>> ageField.addEventListener(TextEvent.TEXT_INPUT, convertAge);
>>> function convertAge (event:TextEvent):Number {
>>>  var resultTxt:TextField = new TextField();
>>>  resultTxt.border = true;
>>>  addChild(resultTxt);
>>>
>>>  var userAge:Number;
>>>  var ageStr:String;
>>>  ageStr = ageField.text;
>>>   userAge = parseInt(ageStr);
>>>  userAge.toString();
>>>  resultTxt.text = String(userAge);
>>>  return userAge;
>>> }
>>>
>>>
>>>      _______________________________________________
>>> Flashcoders mailing list
>>> Flashcoders@chattyfig.figleaf.com
>>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>>
>>>
>>>
>>
>> _______________________________________________
>> Flashcoders mailing list
>> Flashcoders@chattyfig.figleaf.com
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>
>
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to