Tutorial 9-Calculator Lab Exercise
Tutorial 9-Calculator Lab Exercise
1. Create a new Macromedia Flash file by clicking (Ctrl + N) or goes to “File” and
then choose “New”.
2. Create all the buttons that shown at the diagram below; such as (numbers,
punctuations & symbols, and clear buttons). Create the reading bar, by using the
text tool and select the range of your screen based on your preference and select
“Dynamic Text” and type the variable name (Var) as “myNumber” at the
properties area.
Reading Bar:
“Dynamic Text”
3. Create a “Fake Zero” by using the text tool (A) and type “0” and convert it and
choose the symbol as in “Movie Clip”, and rename it as “FakeZero”.
1. Name each of your buttons and “FakeZero” with the names shown at below;
Part 3: Actionscripting:
1. Declaring the instance variable and all the buttons, number press by using the
appropriate logic of the ActionScript.
3. Do the 3 system operations (Clear, Clear Entry and Equalization) by using the
appropriate logic of the ActionScript.
5. Try your best to do the coding and design of the calculator by yourself before
looking at the answer scheme on the next page and sample demo of the calculator
(T9-Calculator).
THE END
Answer Scheme:
Open the “T9-calculator.fla” file, point to actions layer, press F9 and for this round, you
do not have to type the codes, just highlight and copy the following scripts. Be reminded
to study the code.
________________________________________________________________________
myOp="";
lastNumber=0;
thisNumber=0;
myNumber="";
newNum=true;
my1.onRelease= function () {
numberPress(1);
}
my2.onRelease= function () {
numberPress(2);
}
my3.onRelease= function () {
numberPress(3);
}
my4.onRelease= function () {
numberPress(4);
}
my5.onRelease= function () {
numberPress(5);
}
my6.onRelease= function () {
numberPress(6);
}
my7.onRelease= function () {
numberPress(7);
}
my8.onRelease= function () {
numberPress(8);
}
my9.onRelease= function () {
numberPress(9);
}
myDecimal.onRelease= function () {
numberPress(".");
}
my0.onRelease= function () {
numberPress(0);
}
function numberPress(whichKey) {
if (newNum) {
setProperty("myFakeZero",_visible,false);
myNumber=string(whichKey);
} else {
myNumber=myNumber+string(whichKey);
}
newNum=false;
}
myPlus.onRelease= function () {
myOperator("add");
}
function myOperator(myNewOp) {
thisNumber=Number(myNumber);
if (myOp=="add") {
myNumber=lastNumber+thisNumber;
thisNumber=myNumber;
}
if (myOp=="subtract") {
myNumber=lastNumber-thisNumber;
thisNumber=myNumber;
}
if (myOp=="times") {
myNumber=lastNumber*thisNumber;
thisNumber=myNumber;
}
if (myOp=="divide") {
myNumber=lastNumber/thisNumber;
thisNumber=myNumber;
}
lastNumber=thisNumber;
newNum=true;
myOp=myNewOp;
}
myMinus.onRelease= function () {
myOperator("subtract");
}
myTimes.onRelease= function () {
myOperator("times");
}
myDivide.onRelease= function () {
myOperator("divide");
}
myClear.onRelease= function() {
myOp="";
lastNumber=0;
thisNumber=0;
myNumber="";
newNum=true;
setProperty("myFakeZero",_visible,true);
}
myClearEntry.onRelease = function() {
myNumber="";
newNum=true;
setProperty("myFakeZero",_visible,true);
}
myEquals.onRelease=function() {
thisNumber=Number(myNumber);
newNum=true;
if (myOp=="add") {
myNumber=lastNumber+thisNumber;
}
if (myOp=="subtract") {
myNumber=lastNumber-thisNumber;
}
if (myOp=="times") {
myNumber=lastNumber*thisNumber;
}
if (myOp=="divide") {
myNumber=lastNumber/thisNumber;
}
lastNumber=0;
myOp="";
}
________________________________________________________________________
That's all, run your movie and check it all works. If not go back though this tutorial and
check you have not missed anything.
THE END