A really easy task, however, a very common mistake is often made.
When defining the numbers in actionscript, you MUST define them as numbers; otherwise they’ll be recognised as strings.
If they are recognised as strings, 2+2 would come out as 22.
So let’s start this tutorial, and do it the CORRECT way.
Start by selecting the ‘text tool’, in the property box, set it to, ‘Input text’, and draw out a text box. Now right click on the text box you just inserted, and select ‘copy’, right click somewhere else on the stage and select ‘paste’.
Now position the two text boxes next to each other, leave enough room for ‘+’ symbol to go in between them.
Layout so far:
‘+’
Now give the input textbox on the left an instance name of ‘value1’, and the one on the right an instance of ‘value2’. (Do not include the quotes)
Now underneath the two textboxes and ‘+’ symbol, put a ‘=’ symbol in, position it underneath the ‘+’ symbol.
Now using the ‘text tool’ again, insert a textbox underneath the ‘=’ symbol. (This is where the answer will be displayed). Select the text box, and in its property box, set it to ‘dynamic text’, and give it an instance name of ‘answer’, without the quotes.
Layout:
‘+’
‘=’
Right click on the ‘+’ symbol, and select ‘convert to symbol’, select the ‘button’ radio button, and then click OK.
Right click on the ‘=’ button, and select ‘actions’, and insert the following code:
on (release) {
_root.answer = Number(value1)+Number(value2);
}
This tells flash that when the button is released, value1 (the left textbox) and value2 (the right textbox) are to be added together, and the answer is to be displayed in the answer textbox.
Now right click on frame 1, select ‘actions’, and insert the following code:
var value1:Number = 0;
var value2:Number = 0;
var answer:Number = 0;
This sets all the textboxes to 0
And that’s all you need to do.
No comments:
Post a Comment