Result of my flash calculator.
Design idea in ActionScript 3.0:
1) Create a background image Movie Clip bg (no buttons and operators) and linkage to class bg:
//create background instance
var calcbg:bg=new bg();
//Define background x coordinate
//calcbg.x=0;
//Define background y coordinate
calcbg.y=0;
//Define background transparency
calcbg.alpha=0.9;
// Show background on stage
addChild (calcbg);
2) Create a display output box Movie Clip resultbg and linkage to class resultbg:
//create output box instance
var rbg:resultbg=new resultbg();
//Define output box x coordinate (cm):
//rbg.x=20;
//Define output box y coordinate
rbg.y=40;
//Define the width of output box
rbg.width = 200;
//Define the height of the output box
rbg.height = 20;
// Show output box on stage
addChild (rbg);
3) Add Button and Label from component to library.
//Import Button and Label class from library
import fl.controls.Button;
import fl.controls.Label;
// Create "+" Button
var ADD:Button = new Button();
//Create "-" Button
var MINUS:Button = new Button();
//Create "X" Button
var MULTIPLY:Button = new Button();
//Create "/" Button
var DIVIDE:Button = new Button();
//Create "=" Button
var EQUAL:Button = new Button();
//Create "clear" Button
var CLS:Button = new Button();
//Add lable "+" on ADD button
ADD.label = "+";
// When mouse click Add button, operatorClick function is called
ADD.addEventListener (MouseEvent.CLICK, operatorClick);
//Show ADD button on stage
addChild (ADD);
//Define
ADD.x = 145;
ADD.y = 70;
ADD.width = 30;
ADD.height =30;
//The same process is applied to other buttons
var operator:String = "";
//Check if the input is a new number
var numnew:Boolean = true;
//Create "=" Button
EQUAL.label = "=";
//Call equalClick when "+" button is clicked
EQUAL.addEventListener (MouseEvent.CLICK, equalClick);
addChild (EQUAL);
//Action when clicking operator button
function operatorClick (e:MouseEvent) {
EQUAL.dispatchEvent (new MouseEvent(MouseEvent.CLICK));
// Once dispatched, the target property of the event object is set to the object on which //dispatchEvent() was called Used for e.target
operator = Button(e.target).label;
numnew = true;
}
4. Operator and results
//Output result
var resultField:TextField = new TextField();
//When click "=" button
function equalClick (e:MouseEvent) { if ("" == operator) { num1 = Number(resultField.text); } else if ("+" == operator) { num2 = Number(resultField.text); num1 += num2; } else if ("-" == operator) { num2 = Number(resultField.text); num1 -= num2; } else if ("×" == operator) { num2 = Number(resultField.text); num1 *= num2; } else if ("÷" == operator) { num2 = Number(resultField.text); num1 /= num2; } resultField.text = String(num1); numnew = true; operator = "";}
5. Create nine buttons for 1 to 9 digits
for (var i = 0; i<=9; i++) {
numbutt[i] = new Button();
numbutt[i].label = i;
numbutt[i].addEventListener (MouseEvent.CLICK,numClick);
addChild (numbutt[i]);
}
// distribut them in different positions
for (var i:int = 0; i<=9; i++) {
numbutt[i].width = 30;
numbutt[i].height = 30;
if (i>0) {
numbutt[i].x=5+40*((i-1)%3)+20;
numbutt[i].y=30+40*int((i-1)/3)+40;
} else {
numbutt[i].x=5+calcx;
numbutt[i].y=150+calcy;
}
//Add them on stage
for (var i = 0; i<=9; i++) {
numbutt[i] = new Button();
numbutt[i].label = i;
numbutt[i].addEventListener (MouseEvent.CLICK,numClick);
addChild (numbutt[i]);
}
//When the number button is click
function numClick (e:MouseEvent) { if (numnew) { resultField.text =""; } if (resultField.length<29) { resultField.appendText (Button(e.target).label); } else { resultField.appendText (""); } numnew = false;}