|
The God Father
|
Join Date: Jul 2008
Location: NDC
Posts: 5,425
|
04-12-2008, 02:54 AM
|
Flash Calculator AS2
Download the Source (.fla) Files
Step 1: Create a Blank Flash Document
Start by creating a new Flash Document about 200x300 pixels, make sure you set it to ActionScript 2.0
Step 2: Create 2 frames: set actionscrip in 2nd to stop();
The first frame is for actionscript to be added later, and add stop(); to second so you can stop it from looping continuously

Step 3: Create a background for your calculator and put it in frame 2
Just create a simple or self-designed background for your calculator like so... make sure it is all in frame 2

Step 4: Create 2 dynamic texts (one for storage, small) (one for the main, big)
You will have one text to show the current value on the calculator which is the big text that takes up the whole screen and then another text to show the current memory value...
Name the big one welcome with the variable set to tempVal
Set the small variable to mStore, you dont need to name it

Step 5: Create 20 buttons to the calculator
Design them however you want, but below are the different ones you need
0, 1, 2, 3, 4, 5, , 7, 8, 9, +, -, x, /, =, ., c, m+, m-, mrmc

Step 6: Put the following code in the first frame
Click frame 1 and put the following code in it, this is for setting the intial variable values
ActionScript Code
selBut = "none"; // to set operator
tempVal = 0; // main variable
valOne = 0; // first operand
valTwo = 0; // second operand
decIn = 0; // for setting decimal
decCount = 0; // for setting decimal place
mStore = 0; // memory storage
mReset = 0; // for resetting memory
Step 7: Add the following code to the second frame
This code is the functions for setting the numbers and setting the operator...
ActionScript Code
// Set number to put into main variable
// welcome.text.length is the length so it doesnt go off screen
function setNumber (number) {
if(welcome.text.length < 8 && decIn == 0){
tempVal = (tempVal*10)+number;
}else if(welcome.text.length < 8){
tempVal = tempVal + (number/(Math.pow(10,decCount)));
tempNum = Math.pow(10,decCount);
decCount++;
}
}
// Set operator to div, mul, add, or sub
// save first operand and reset main
function setOperator (operator) {
selBut = operator;
valOne = tempVal;
tempVal = 0;
decIn = 0;
decCount = 0;
heyBut.enabled = true;
}
stop(); // to stop frames
Step 8: Set the following code to each number button and each operator button (+-x/)
Make sure you change the variable you pass for each button!
ActionScript: Numbers
on(release){
setNumber(0); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
ActionScript: Operators
on(release){
setOperator("mul"); // mul, div, add, or sub
}
Step 9: Set the decimal button with the following code
This is for adding a decimal to the number
ActionScript: Decimal
on(release){
if(welcome.text.length < 8 && decCount == 0){
decIn = 1;
decCount = 1;
}
}
Step 10: Set the clear button with the following code
This is for clearing the calculator
ActionScript: Decimal
on(release){
selBut = "none";
tempVal = 0;
valOne = 0;
valTwo = 0;
decIn = 0;
decCount = 0;
}
Step 11: Set the m+, m-, mrmc buttons with the following code
This involves all the memory
ActionScript: m+
on(release){
mStore += tempVal;
mReset = 1;
}
ActionScript: m-
on(release){
mStore -= tempVal;
mReset = 1;
}
ActionScript: mrmc
on(release){
if(mReset == 0){
mStore = 0;
}else{
tempVal = mStore;
mReset = 0;
}
}
Step 12: Lastly, set the = button to the following code
This is what does all the math for the calculator...
ActionScript: Decimal
on(release){
valTwo = tempVal;
if(selBut == "div"){
newVal = (valOne) / (valTwo);
}
if(selBut == "mul"){
newVal = (valOne) * (valTwo);
}
if(selBut == "add"){
newVal = (valOne) + (valTwo);
}
if(selBut == "sub"){
newVal = (valOne) - (valTwo);
}
// for rounding the variable to 4 decimal places
tempVal = int((newVal)*10000)/10000;
heyBut.enabled = false;
}
VIEW THE RESULT BELOW
__________________
I Love Walking In The Rain Cuz Nobody Know I'm Crying !!
|
|