Video Tutorial ActionScript Calculator Part 11: Final! Display Results – Switch Case on ActionScript

Fri, Sep 25, 2009

Adobe Flash


Here is a fully working Calculator. Feel Free to Test.

This movie requires Flash Player 9

We finalize the calculator here, using a select case statement, to find the requested operation and display the result on the window.
Click here to see the caculator deployed on Widgetbox performing page views and downloads

Click here for the Final, Completed, Flash CS4 Source Code: $5

HERE IS THE SOURCE CODE!

//The ActionScript 3.0 Calculator Complete
// solutions at say-web dot com
// FREE to USE, COPY and DISTRIBUTE

var numbers:Array= [ num0_btn, num1_btn, num2_btn, num3_btn, num4_btn, num5_btn, num6_btn, num7_btn, num8_btn, num9_btn];
var operators :Array = [add_btn, subtract_btn, multiply_btn, divide_btn, equal_btn];
var op:String;
var num1:Number;
var num2:Number;

//this defines the click sound location and loads it
var sound:Sound = new Sound();
var req:URLRequest = new URLRequest (“http://say-web.com/SWF/click.mp3″);
sound.load(req);

function addListeners():void
{

for (var i:uint=0; i < numbers.length; i++){
numbers[i].addEventListener(MouseEvent.CLICK,pressNumber);}

for (i=0;i

operators[i].addEventListener(MouseEvent.CLICK,pressOperator);

}

clear_btn.addEventListener(MouseEvent.CLICK, clearAll);
dot_btn.addEventListener(MouseEvent.CLICK,addDot);

}

function pressNumber(event:MouseEvent):void {
var instanceName:String=event.target.name;
var numPushed= instanceName.charAt(3);
if (window_txt.text =="0" || num1 == Number(window_txt.text)) {
//This will remove the initial zero on the screen
window_txt.text=""; }

window_txt.appendText(numPushed);
sound.play();
}

function pressOperator(event:MouseEvent):void {
sound.play();
var instanceName:String= event.target.name;
var currentOp:String;
// This will store the current operation

currentOp = instanceName.slice(0,instanceName.indexOf("_"));

if (!num1){

num1= Number(window_txt.text);
window_txt.text = "";
op = currentOp;

}

else if (!num2){
num2=Number(window_txt.text);
showCalculation();
op = currentOp;

}
}
function clearAll(event:MouseEvent):void {
// This will put the zero back in the text box
// and erase any possible value to be processed

window_txt.text = "0";
num1 = NaN;
num2 = NaN;

// This will play the click sound
sound.play();

}
function addDot(event:MouseEvent):void {
//this will place a zero when asking for
// a decimal dot

if (num1 == Number(window_txt.text)){

window_txt.text= "0"; }

//The indexOf property will return -1
// if a given character is not part of a string

if (window_txt.text.indexOf(".")==-1){

window_txt.appendText(".");}

// This will play the click sound
sound.play();

}

function showCalculation():void{

switch (op){

case "multiply":
num1*=num2;
break;

case "divide":
num1/=num2;
break;

case "subtract":
num1-=num2;
break;

case "add":
num1+=num2;
break;

default:
break;

}

//now that we found out the result
//let's display on the window

window_txt.text=String(num1);
num2=NaN;

}

window_txt.text= "0";
addListeners();

Related Articles:

, , , , , , , , , , , , , ,

2 Responses to “Video Tutorial ActionScript Calculator Part 11: Final! Display Results – Switch Case on ActionScript”


Trackbacks/Pingbacks

Leave a Reply

You must be logged in to post a comment.

My name is Avraham Saltoun, and I live in Jerusalem, Middle East. I am currently on my mid  40's and I am a computer programming technician. Here you will find over one hundred quality video tutorials on Actionscript, and yes, I give online support for aspiring programmers, and pros as well, sharing screen on skype and delivering source code by email, click the button for live flash actionscript support now!

 
oDesk Certified Action Script Programmer
oDesk Certified Adobe Flash CS3 Designer
oDesk Certified Adobe Photoshop 6.0 Designer
Skype Me™!

$7.49 .Com! Score Savings Cartoon Smart Tutorials


1,260
Unique Visitors
Previous 30 Days
Powered By Google Analytics