While I am having you people around, I might take advantage and ask the following:
I have 2 buttons and a textfield. I click the button A to input a number in the textfield, and then I click the button B to give me let's say the square root of that number in the same textfield. I have been stuck for 3 days. help!
Correct me please. I came up with this. It seems to work.
import flash.events.Event;
// Defining a TextField object instance
var txt_inp:TextField = new TextField();
txt_inp.type = "input";
txt_inp.x = 100;
txt_inp.y = 50;
txt_inp.width = 100;
txt_inp.height = 20;
txt_inp.border = true;
txt_inp.borderColor = 0x0000da;
txt_inp.restrict = "0-9";
txt_inp.maxChars = 5;
txt_inp.multiline = false;
txt_inp.text = '';
addChild(txt_inp);
buttonA.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
trace("Mouse clicked");
txt_inp.text = " 9";
}
buttonB.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
trace("Mouse clicked");
var res:Number = Math.sqrt(Number(txt_inp.text));
//if(isNaN(res)) res = -1;
txt_inp.text = String(res);
}
Since it is an input field and you haven't restricted typing into it, you should be able to "type" any values you wish into it. If when you say "type" you are actually talking about clicking your button that assigns the String " 9" to the textfield, your code for that button is only assign that String, so it cannot do any better than that. If you want to append another 9 to the String in the textfield, then you need to use the appendText() method. But appending " 9" to " 9" will end up giving you " 9 9" which will not be a number value.
Try the following instead of what you have...
import flash.events.Event;
// Defining a TextField object instance
var txt_inp:TextField = new TextField();
txt_inp.type = "input";
txt_inp.x = 100;
txt_inp.y = 50;
txt_inp.width = 100;
txt_inp.height = 20;
txt_inp.border = true;
txt_inp.borderColor = 0x0000da;
txt_inp.restrict = "0-9";
txt_inp.maxChars = 5;
txt_inp.multiline = false;
txt_inp.text = '';
addChild(txt_inp);
buttonA.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
txt_inp.appendText("9");
}
buttonB.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);
function fl_MouseClickHandler_2(event:MouseEvent):void
{
trace("Mouse clicked");
var res:Number = Math.sqrt(Number(txt_inp.text));
txt_inp.text = String(res);
}
North America
Europe, Middle East and Africa
Asia Pacific