Here is a fully working Calculator. Feel Free to Test.
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!
// 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); } function pressNumber(event:MouseEvent):void { window_txt.appendText(numPushed); function pressOperator(event:MouseEvent):void { currentOp = instanceName.slice(0,instanceName.indexOf("_")); if (!num1){ num1= Number(window_txt.text); } else if (!num2){ } window_txt.text = "0"; // This will play the click sound } if (num1 == Number(window_txt.text)){ window_txt.text= "0"; } //The indexOf property will return -1 if (window_txt.text.indexOf(".")==-1){ window_txt.appendText(".");} // This will play the click sound } function showCalculation():void{ switch (op){ case "multiply": case "divide": case "subtract": case "add": default: } //now that we found out the result window_txt.text=String(num1); } window_txt.text= "0";
dot_btn.addEventListener(MouseEvent.CLICK,addDot);
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=""; }
sound.play();
}
sound.play();
var instanceName:String= event.target.name;
var currentOp:String;
// This will store the current operation
window_txt.text = "";
op = currentOp;
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
num1 = NaN;
num2 = NaN;
sound.play();
function addDot(event:MouseEvent):void {
//this will place a zero when asking for
// a decimal dot
// if a given character is not part of a string
sound.play();
num1*=num2;
break;
num1/=num2;
break;
num1-=num2;
break;
num1+=num2;
break;
break;
//let's display on the window
num2=NaN;
addListeners();






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