flash 8 textfield listeners calculations
this is a little snatch of actionscript code that will listen to a textfield and calculate an equation according to a textbox input..
line1_txt is the input, line2_txt is the feedback and line3 is more feedback..
The way to change an input from a textbox (a String ) to a number that you can use in a calculation or equation is
parseFloat(”string”);
parseFloat(”string”) converts a string (e.g. “23.45″ ) to a Number datatype.
a Float is a decimal number . e.g. 10.32
you can use parseInt(”string”) if you like as well if you are using whole numbers
an Integer (int) is a whole number ( 1, 2, 3, 6, 432, 101 etc) and not a decimal.
usage example
var m:Number = parseFl0at(textfield_txt.text);
m = 0.5 x m;
trace(m);
// ————– start code ——————– //
line1_txt.text = ” “;
line2_txt.text = ” 0.5 x D = M”;
var D:Number;
// line1_txt, line2_txt, and line3_txt
//define an onChanged handler for when text changes in the input text field
line1_txt.onChanged = function(line1_txt) {
};
//construct the listener object
myListenerObject = new Object();
myListenerObject.onChanged = function(line1_txt) {
var m = parseFloat(line1_txt.text);
m = m / 2;
line2_txt.text = ” .5 x ” + line1_txt.text + ” = ” + m ;
line3_txt.text = ” ” + m;
};
//call the TextField.addListener method to register the object
line1_txt.addListener(myListenerObject);
// ——————- end code ———————- //
