Anda di halaman 1dari 6

FLASH TUTORIAL 9 APU

MACROMEDIA FLASH Tutorial 9:


ACTIONSCRIPTING IN CALCULATOR
Part 1: Create Calculator’s Buttons and Reading Bar:

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”.

INTRO TO MULTIMEDIA APPLICATIONS (CT004-3-0-IMA) 1


FLASH TUTORIAL 9 APU

Convert the symbol


into a “Movie Clip”
and rename it as
“FakeZero”

Part 2: Naming Convention:

1. Name each of your buttons and “FakeZero” with the names shown at below;

Btn 0 = my0 Btn Times = myTimes


Btn 1 = my1 Btn Divide = myDivide
Btn 2 = my2 Btn Clear Entry =
Btn 3 = my3 myClearEntry
Btn 4 = my4 Btn Clear = myClear
Btn 5 = my5 Btn Minus = myMinus
Btn 6 = my6 Btn Plus = myPlus
Btn 7 = my7 Btn Equals = myEquals
Btn 8 = my8 Movie Clip FakeZero =
Btn 9 = my9 myFakeZero
Btn . = myDecimal

Part 3: Actionscripting:

1. Declaring the instance variable and all the buttons, number press by using the
appropriate logic of the ActionScript.

2. Do the 4 main Mathematical operations (Addition, Subtraction, Multiplication,


and Division) by using the appropriate logic of the ActionScript.

INTRO TO MULTIMEDIA APPLICATIONS (CT004-3-0-IMA) 2


FLASH TUTORIAL 9 APU

3. Do the 3 system operations (Clear, Clear Entry and Equalization) by using the
appropriate logic of the ActionScript.

4. Now press (Ctrl+Enter) to view and test your Calculator.

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).

Try to do it on yourself and do not cheat!

THE END

INTRO TO MULTIMEDIA APPLICATIONS (CT004-3-0-IMA) 3


FLASH TUTORIAL 9 APU

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;
}

INTRO TO MULTIMEDIA APPLICATIONS (CT004-3-0-IMA) 4


FLASH TUTORIAL 9 APU

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;

INTRO TO MULTIMEDIA APPLICATIONS (CT004-3-0-IMA) 5


FLASH TUTORIAL 9 APU

}
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

INTRO TO MULTIMEDIA APPLICATIONS (CT004-3-0-IMA) 6