Skip to main content
Known Participant
March 15, 2011
Answered

simple sum 2 numbers and display

  • March 15, 2011
  • 1 reply
  • 4156 views

Hello all, well I'm new in AS3 and I was looking, but didnt find solution, so I hope that someone can help me since this mast be easy for people who work with AS3.

My question is how to make 2 input text fileds(numbers) and one dynamic text field which will display result of sum of that 2 input text fields(numbers)?

"FirstInputTextField+SecondInputTextField = display Sum in dynamic text field"

I hope that I'm explain right since my english isnt so good.

Regards,

SEA_Elvir

This topic has been closed for replies.
Correct answer Ned Murphy

If you want it to happen automatically when you enter values you will need to have event listeners assigned to the two input textfields as well.

input1.addEventListener(Event.CHANGE, sumInputs);
input2.addEventListener(Event.CHANGE, sumInputs);

function sumInputs(evt:Event):void {
    if(input1.length > 0 && input2.length > 0){
       dynamic1.text = String( Number(input1.text) + Number(input2.text) );
    }
}

That code only checks to see that both inputs contain something, so you will have to add more conditions if you wish to have more control of what is considered as valid input.

1 reply

Ned Murphy
Legend
March 15, 2011

The general form you would follow is...

dynamic1.text = String( Number(input1.text) + Number(input2.text) );

where dynamic1, input1, and input2 represent the instance names you assign to the textfields that you add to the stage using the text tool.

SEA_ElvirAuthor
Known Participant
March 15, 2011

Thank you Ned, but I put as you write code and give instance names for input1 = Input Text and input2 = Input Text and dynamic1 put Dynamic Text and when I test movie I get 0 for dynamic1, but when I put some value to intupt1 and input2 nothing happened?

Thank you again

Ned Murphy
Ned MurphyCorrect answer
Legend
March 15, 2011

If you want it to happen automatically when you enter values you will need to have event listeners assigned to the two input textfields as well.

input1.addEventListener(Event.CHANGE, sumInputs);
input2.addEventListener(Event.CHANGE, sumInputs);

function sumInputs(evt:Event):void {
    if(input1.length > 0 && input2.length > 0){
       dynamic1.text = String( Number(input1.text) + Number(input2.text) );
    }
}

That code only checks to see that both inputs contain something, so you will have to add more conditions if you wish to have more control of what is considered as valid input.