Skip to main content
Participant
August 4, 2011
Question

AS3 student needs help!

  • August 4, 2011
  • 1 reply
  • 299 views

Hi readers I'm new to AS3 and Im not too sure who to ask for help so I thought I would try here first! (If some can recommend a better place please do!)

I'm trying to work out txt boxes....
I cant figure out what's going wrong here.
I wasn't able to use the Number variable to store inputs from the text boxes. So I looked up how to convert them to strings... I thought that would solve all my problems but now I just get a return of "NaN" when I press my button. Anyone know what's wrong with my Script??


import flash.events.MouseEvent;
// HomeWork week 2 - Jack McCallum - 3/8/2011
// Take input from 4 text fields. The number of fruit and payment.
// Output in a textbox the change from the transaction

stop(); //a wee stop button
var pricePlum:Number = 2.0;
var priceGrape:Number = 2.10;
var pricePeach:Number = 2.90;
// super cool variable names go here
var numPlum = Number(input1);
var numGrape = Number(input2);
var numPeach = Number(input3);
var payment = Number(input4);
var changy:Number;
// It wouldnt let me input using "number" variable so I tryed coverting them all to strings and then ouput as strings
var input1:String;
var input2:String;
var input3:String;
var input4:String;
var outputText = String(changy);

fruitButton.addEventListener(MouseEvent.CLICK, nextClick);//if I click the button stuff happens

//below is the main function wich calls the other functions. it is suposed to take buyFruit ouput and put it in a text box.
function nextClick(myNextEvent:MouseEvent):void{

captureText();
buyFruit();
textChange.text = outputText;
}
// hmmm why wont you work?
function captureText():void {

input1 = textPlum.text;
input2 = textGrape.text;
input3 = textPeach.text;
input4 = textPayment.text;

}
//calculates the change by: payment minus number of fruit times price etc.
function buyFruit():Number
{
return changy = payment -((numPlum * pricePlum)+(numGrape * priceGrape)+(numPeach * pricePeach));
}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
August 4, 2011

the text property of a textfield is always a string, even if it looks like a number to you.  to coerce flash into treating what looks like a number to you to a number that flash can handle use the Number() function:

var n:Number=Number(yourtextfield.text);

also, make sure your textfields are NOT multiline.

Participant
August 4, 2011

YES! that was helpful I was doing alot of unessecary String stuff there. I didnt need those extra variables! problem solved! thankyou!

kglad
Community Expert
Community Expert
August 4, 2011

you're welcome.