Adobe Flash Professional CS6 Adobe Flash Professional CS6 Classroom In A Book | Page 290
5 Create an event listener and function for the Calculate button. You should
be familiar with event listeners from Lesson 6, but if not, you should review
the concepts in that lesson before moving on. The event listener and function
should appear as shown here:
6 Enter code within the function to make the mortgage calculations and display
the results. The completed code for the event listener and function should be
as follows:
calculate_btn.addEventListener(MouseEvent.CLICK,
calculatemonthlypayment);
function calculatemonthlypayment(e:MouseEvent):void {
var loan:Number=price-Number(down_txt.text)/100*price;
var c:Number=Number(rate_txt.text)/1200;
monthlypayment = loan*(c*(Math.pow((1+c),term)))/(Math.
pow((1+c),term)-1);
monthly_txt.text=String(Math.round(monthlypayment));
}
Don’t get too discouraged looking at the code! Take your time to copy it exactly,
or you can copy and paste it from the 07End.fla file in the 07End folder.
It may look complicated, but there are only two important concepts to identify. First,
the contents of text boxes are referenced by the text property. So down_txt.text
refers to the contents in the text box named down_txt , and rate_txt.text refers
to the contents in the text box named rate_txt .
Second, text boxes contain text, or String data. To make numeric calculations, you
must first convert the text to a number using Number() . To convert a number back
to text, use String() .
The rest of the surrounding code is algebraic manipulation using addition, sub-
traction, multiplication, division, and exponents according to a straightforward
mortgage payment formula.
Adobe FLAsh ProFessionAL Cs6 CLAssroom in A book
281