I recieved an email this morning asking me about building a calculator in flash.
Here is the email:
hey i need some help plz (with actionscript)
how do i make a calculator?
can u write step by step plz
(and by the way i’m <…>
an i got ur addy form a forum)
You might be thinking that he was quite rude about it. I certainly did and my reply email said that a lot and then as not to be too rude, I actually tried to help the guy out.
You might be thinking that this is very nice of me, but you didn’t read the reply
I don’t know where he got my email from, it might not have been from this forum but I thought that I would post it here anyway.
If you spot any bugs, please post about how to recreate them.
Preview here: ImageShack - Best place for all of your image hosting and image sharing needs
You’ll notice I chose to use the mathematical approach over the more simple Number(string) approach. Makes things more exciting ! :thumb:
/*
SETTING UP THE STAGE
To set up the stage I created buttons for the following actions (with their relative instance name in brackets)
--------------------------------------------
Button Instance Name Action
--------------------------------------------
0 input_0 0
1 input_1 1
2 input_2 2
3 input_3 3
4 input_4 4
5 input_5 5
6 input_6 6
7 input_7 7
8 input_8 8
9 input_9 9
. action_point Start adding to decimals
= action_equals Calculate Answer
+ action_add) Add the new number
- action_subtract Subtract the new number
* action_multiply Multiply the new number
/ action_divide Divide the new number
C action_clear Clear the current input
CE action_clearall Clear the current solution
1/-1 action_state Changes the current input between +/- it's value
--------------------------------------------
And then I created a textfield with variable property set to "output"
This means that the textfield I created will display whatever is in the variable `output`.
There is also a textfield with variable property set to "action" to show the current method being used.
*/
/*
VARIABLES
*/
// The contents of this variable is shown in the textfield
var output:String = "";
/*
The following variables are used to calculate the effect of inputing a new number.
I'll talk a little about the mathematics behind it.
When we want to add a number to the end of a new number, for example: 123 becomes 123,
we multiply our original number (123) by 10 and then add the new number (4).
When we want to add a decimal to the end of a new number, for example: 123.4 becomes 123.45,
we multiply the new number (5) by 10^-(# decimal places). However there are no calculaations required for this.
The method we will assume will update the multiplier of the new number with every number after the decimal point that is added.
Upon each update of this multiplier, the multiplier is divided by 10.
The variables are currently set up for appending new numbers to the end of the the current number
When the point button is pressed they are changed to reflect their new requirements
*/
var multiplier_old:Number = 10;
var multiplier_new:Number = 1;
// This variables shows that the point buttons has/hasn't been pushed
var point:Boolean = false;
// This variables stores what action we're going to take when we update, whether it be +,-,* or /
var action:String = "";
// The current solution (updated with every new action submitted)
var solution:Number;
// The current number in the textfield
var input:Number = 0;
// The current input's state (positive / negative);
var state:Number = 1;
/*
BUTTON ACTIONS
Each input button will call a function with it's relative number, to avoid having to copy and paste all of the working code.
*/
input_1.onPress = function() { inputNumber(1); }
input_2.onPress = function() { inputNumber(2); }
input_3.onPress = function() { inputNumber(3); }
input_4.onPress = function() { inputNumber(4); }
input_5.onPress = function() { inputNumber(5); }
input_6.onPress = function() { inputNumber(6); }
input_7.onPress = function() { inputNumber(7); }
input_8.onPress = function() { inputNumber(8); }
input_9.onPress = function() { inputNumber(9); }
input_0.onPress = function() { inputNumber(0); }
// =: Runs the calculation function. the `true` parameter tells the function to unset `solution`
action_equals.onPress = function() { calculate(true); };
// .: Sets the multipliers so that new input numbers become decimals of a lower unit column
action_point.onPress = function() {
multiplier_old = 1;
multiplier_new = 0.1;
point = true;
};
// 1: Changes the state between 1 and -1;
action_state.onPress = function() { state *= -1; }
// *: Each button of type +-*/ calls the setAction function with the appropriate symbol for their action
action_add.onPress = function() { setAction("+"); };
action_divide.onPress = function() { setAction("/"); };
action_subtract.onPress = function() { setAction("-"); };
action_multiply.onPress = function() { setAction("*"); };
// C: Clears the current input for use in the event of a mistake being input. Basically just resets the variables
action_clear.onPress = function() {
input = 0;
point = false;
multiplier_old = 10;
multiplier_new = 1;
state = 1;
output = input.toString();
}
//CE: Same as C but clears the solutions and actions - total reset. Calls the action_clear.onPress function instead of copying in the code again.
action_clearall.onPress = function() {
solution = undefined;
action = "";
action_clear.onPress();
}
// Takes intput from the input_ buttons and adds it to the input after applying the multipliers in the method described above.
// If `point` is true then the multiplier_new is divided by 10, also as described.
function inputNumber(n:Number):Void {
input = input*multiplier_old + n*multiplier_new;
if (point) multiplier_new *= 0.1;
output = input.toString();
}
// Sets the current action in the `action` variables.
// The call to calculate() simply updates the input and solution variables (more in the calculate function explanation)
function setAction(a:String):Void {
calculate();
action = a;
}
/*
CALCULATE FUNCTION
This function calculates how to update the solution.
If there currently is no solution, ie. this is the first time that an action button has been pressed,
then the solution is simply going to be the input and is set accordingly.
When there is already a solution specified, ie. this call is after the first call - something has been updated,
then the solution is updated according to the action.
After either of these events, the solution is put into the output display and then variables are reset for the next input's usage.
*/
function calculate(end:Boolean):Void {
input *= state;
if (solution == undefined) {
solution = input;
} else {
switch(action) {
case "+":
solution += input;
break;
case "-":
solution -= input;
break;
case "*":
solution *= input;
break;
case "/":
solution /= input;
break;
}
}
output = solution.toString();
input = 0;
point = false;
multiplier_old = 10;
multiplier_new = 1;
state = 1;
action = "";
if (end) solution = undefined;
}
Hope this helps somebody.
The FLA is in MX2004 format.