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:

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

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


Trackbacks/Pingbacks

  1. Диваны в Екатеринбурге…

    Thanks for the marvelous posting! I actually enjoyed reading it, you could be a great author. I will make certain to bookmark your blog and definitely will come back down the road. I want to encourage you to continue your great posts, have a nice eveni…

  2. others who may find value in it…

    as well. feel free to tweet it also.so that i may keep the content current and informative i’d appreciate it you also left your comments. that would be great.make it a great day.p.s. the majority of the content above can be…

  3. the town if you strive to share…

    nothing but amazing information each time you tap on your keyboard. offer expert advise (your readers will love you for this), answer frequently asked questions, and share trade secrets and insider tips. these are the things that you need to do…

  4. power tools says:

    Good Read…

    Definately Being Bookmarked!…

  5. rooftops, you’ll be able to show how…

    your solutions will make their life so much easier. you’ll be addressing your target market’s needs directly.qualify and funnel.in terms of qualifying and funneling your website visitors, you’ll only ever be able to achieve this if you present the r…

  6. bold and bright picture (especially if you’re…

    sharing your post on facebook or in blogging communities) and don’t forget to optimize the image size and file size before uploading it to your blog.that’s it – there’s plenty of fantastic free images to be found on the web -…

  7. or post your first article as soon…

    as possible, just write it directly from there or just paste your title and the article body. you can also add an image, enter your tags and categories then click to publish instantly.if you have further questions regarding html codes, track…

  8. probably the best way to get into…

    online marketing as it can be highly ethical. you will supply quality content to people who will thank you for it and eventually you will be awarded for it too.a professional blog requires a lot of time invested into it and…

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


839
Unique Visitors
Previous 30 Days
Powered By Google Analytics