Hi,
What I have is two buttons that when clicked they add an input text field and a third button that puts a text field and gives you the total value of the two input fields after the user inputs some numbers, the problem is that the buttons adding the input fields can be clicked multiple times and of course adding a new input field each time they are clicked and as you can imagine when I try to add the values I only get the result of the last two fields added.
How can I tweak my code so it adds-up the total value of all input fields on the stage?
Sorry I didn’t test my code it may have some errors, I just put it together to show the concept.
Thanks a lot!
button1.addEventListener(MouseEvent.CLICK, addField);
button2.addEventListener(MouseEvent.CLICK, addField2);
button3.addEventListener(MouseEvent.CLICK, addTotal);
var txtInput1:TextField;
var txtInput2:TextField;
var txtTotal:TextField;
var txtWidth=100;
var txtHeight=20;
function addField(event:MouseEvent):void{
txtInput1 = new TextField();
txtInput1.type=TextFieldType.INPUT;
txtInput1.width = txtWidth;
txtInput1.height = txtHeight;
txtInput1.background = true;
txtInput1.border = true;
txtInput1.x = 200;
txtInput1.y = numChildren * (texHeight+10);
addChild(txtInput1);
}
function addField2(event:MouseEvent):void{
txtInput2 = new TextField();
txtInput2.type=TextFieldType.INPUT;
txtInput2.width = txtWidth;
txtInput2.height = txtHeight;
txtInput2.background = true;
txtInput2.border = true;
txtInput2.x = 200;
txtInput2.y = numChildren * (texHeight+10);
addChild(txtInput2);
}
function addTotal(event:MouseEvent):void{
txtTotal = new TextField();
txtTotal.x = 200;
txtTotal.y = 300;
txtTotal.border= true;
txtTotal.textColor=0xFF0000;
txtTotal.text= Number(txtInput1.text)+Number(txtInput2.text);
addChild(txtTotal);
}