I've been asked to make a calculator in as3 in a day! but i only have the basic understanding skills of as2.
Can anyone help in anyway?
Printable View
I've been asked to make a calculator in as3 in a day! but i only have the basic understanding skills of as2.
Can anyone help in anyway?
What exactly do you need help with?
Sprites? Event handling? Classes? Packages?
i wanted to know if anyone had made one so i can look at the code and understand how it works, so i can make one on my own.
out of my league here...
PM sent
Hi Afternoon Delight,
Would you please send me a copy of that calculator code, too? I have a similar situation.
Much appreciated,
phoolholy:)
HiQuote:
Originally Posted by phoolholy
I have just joined and not sure about the protocol. I think I am talking to phoolholy.
Anyway, I am looking for the code to the calculator. Can you send it to me? Will you need more info?
Regards
indime
Hi, This isn't a complete calculator. Only addition. But it's a start.
var enteredNumber = "";
var theResult = 0;
var theOperation = "";
var firstNumber = 0;
bt0.addEventListener(MouseEvent.CLICK, enterNumber);
bt1.addEventListener(MouseEvent.CLICK, enterNumber);
bt2.addEventListener(MouseEvent.CLICK, enterNumber);
bt3.addEventListener(MouseEvent.CLICK, enterNumber);
bt4.addEventListener(MouseEvent.CLICK, enterNumber);
bt5.addEventListener(MouseEvent.CLICK, enterNumber);
bt6.addEventListener(MouseEvent.CLICK, enterNumber);
bt7.addEventListener(MouseEvent.CLICK, enterNumber);
bt8.addEventListener(MouseEvent.CLICK, enterNumber);
bt9.addEventListener(MouseEvent.CLICK, enterNumber);
btpl.addEventListener(MouseEvent.CLICK, addNumber);
bteq.addEventListener(MouseEvent.CLICK, calculateAnswer);
function enterNumber(event:MouseEvent) {
if (event.currentTarget == bt0) {
enteredNumber = enteredNumber + "0";
txt_result.text = enteredNumber;
}
if (event.currentTarget == bt1) {
enteredNumber = enteredNumber + "1";
txt_result.text = enteredNumber;
}
if (event.currentTarget == bt2) {
enteredNumber = enteredNumber + "2";
txt_result.text = enteredNumber;
}
if (event.currentTarget == bt3) {
enteredNumber = enteredNumber + "3";
txt_result.text = enteredNumber;
}
if (event.currentTarget == bt4) {
enteredNumber = enteredNumber + "4";
txt_result.text = enteredNumber;
}
if (event.currentTarget == bt5) {
enteredNumber = enteredNumber + "5";
txt_result.text = enteredNumber;
}
if (event.currentTarget == bt6) {
enteredNumber = enteredNumber + "6";
txt_result.text = enteredNumber;
}
if (event.currentTarget == bt7) {
enteredNumber = enteredNumber + "7";
txt_result.text = enteredNumber;
}
if (event.currentTarget == bt8) {
enteredNumber = enteredNumber + "8";
txt_result.text = enteredNumber;
}
if (event.currentTarget == bt9) {
enteredNumber = enteredNumber + "9";
txt_result.text = enteredNumber;
}
}
function addNumber(event:MouseEvent) {
theResult = enteredNumber;
enteredNumber = "";
theOperation = "+";
}
function calculateAnswer(event:MouseEvent) {
if (theOperation == "+") {
theResult = Number(theResult) + Number(enteredNumber);
txt_result.text = theResult;
}
}
Thanks
I will use this to build on. I will try and add trig functiions, will post when done.
Cheers
indime
@ phoolholy, your could rewrite your code. It's really AS 2.0-ish...
You could assign that same event listener to all your button with a for each... that way you could reduce a lot of code ; you are using way too much if statements, that could be reduced too...
This is code my teacher gave me. Not the best, I know.
Thanks.
^ haha, never trust your teachers, especially when it comes to new stuff.... 90% of them will never be capable of making that transition: from old to new... they'll be thinking the old way, using new syntax but that not the good way. :) Anyways, don't get me wrong, the code might work, only that it's ideology is old and it's not exploiting AS 3.0's advantages.
oh man that code is insanely atrocious. That whole thing could be rewritten in 20 lines, including imports and placement of all the buttons. Part of the point of programming is to not have to repeat the same task over and over...
Also, computers are natural calculators; just take the string and evaluate it, you don't need a separate function for every operator. And a for/next loop would work nicely to do the redundant stuff...
I agree with joshstrike...that code is chaos. You might also think of using a dictionary to index your buttons against their value
PHP Code:
var codeByButton:Dictionary = new Dictionary();
codeByButton[bt0] = 0;
codeByButton[bt1] = 1;
...
I mean if you had to have the buttons onscreen before and you couldn't generate them with code --
I just realized that won't work 'cause the operations are retroactive...uh...you'd just have to queue the last operation.Code:var currentStr:String = "";
var tot:Number = 0;
var buttons:Array = new Array();
for (var i:Number=0;i<10;i++) {
buttons.push(root["bt"+i]);
buttons[buttons.length-1].name = String(i);
}
var operators:Array = ["+","-","/","*"];
for each (var o:String in operators) {
buttons.push(root["bt"+o]);
buttons[buttons.length-1].name = o;
}
for each (var b:MovieClip in buttons) {
b.addEventListener(MouseEvent.CLICK,this.gotPress,false,0,true);
}
function gotPress(evt:MouseEvent):void {
//something like this; numeric inputs are added to the string
if (!isNaN(Number(evt.target.name))) {
//literally tacked on at the end of the string as a substring.
currentStr += evt.target.name;
} else {
//if no total exists,
!tot ? tot = Number(currentStr) : void;
//eval() is gone =\
switch (evt.target.name) {
case "+":
currentStr = String(Number(currentStr)+tot);
break;
case "-":
currentStr = String(tot-Number(currentStr));
//etc.
}
}
}
}
Whatever.
Point is use a loop to add your buttons and events
How would you reuse the answer once you did a calculation? EX: 4+3=7 + 3=10
I understand you need an array, but how do you implement it?
If there is someone who has the full code for a calculator, I would really appreciate if I could have it. This is way outta my league.
to make multiply, divide and subtract btns what code would i imput?
button_pl.addEventListener(MouseEvent.CLICK, addNumber);
button_e.addEventListener(MouseEvent.CLICK, calculateAnswer);
//*do i imput this?//*
button_di.addEventListener(MouseEvent.CLICK, divideNumber);
button_m.addEventListener(MouseEvent.CLICK, multiplyNumber);
button_su.addEventListener(MouseEvent.CLICK, voidNumber);
//*for the subtracting function would i sub something like this in?//*
function subtractNumber(event:MouseEvent) {
theResult = enteredNumber;
enteredNumber = "";
theOperation = "-";
just a bit new to coding in as3 so it would be nice if anyone could help.
its grand i sorted it out myself.i was actually right and foud out that if you change
}
function calculateAnswer(event:MouseEvent) {
if (theOperation == "+") {
theResult = Number(theResult) + Number(enteredNumber);
txt_result.text = theResult;
//*you just add another if statement//*
if (theOperation == "-") {
theResult = Number(theResult) - Number(enteredNumber);
txt_result.text = theResult;
}
//* it was really that simple//*
Throwing my hat into the ring---
Actionscript Code:import flash.display.MovieClip;
import flash.events.MouseEvent;
const MODE_ADDITION = "addition";
const MODE_SUBTRACTION = "subtraction";
const MODE_DIVISION = "division";
const MODE_MULTIPLICAITON = "multiplicaiton";
var calcMode:String = MODE_ADDITION;
var modeDisplay:String = "+";
var currentNum:String = "";
var total:Number = 0;
function createNumberKey(mc:MovieClip, val:int):void{
mc.val = val;
mc.displayNumber.text = val.toString();
mc.addEventListener(MouseEvent.CLICK, calculateMouseEvent);
}
function calculateMouseEvent(e:MouseEvent):void{
currentNum += e.currentTarget.val.toString();
totalDisplay.text = total.toString() +" "+modeDisplay+" "+currentNum;
return;
}
for (var i:int = 0;i<10;i++){
createNumberKey(this["a"+i],i);
}
createTool(AddTool, MODE_ADDITION,"+");
createTool(SubTool, MODE_SUBTRACTION,"-");
createTool(DivTool, MODE_DIVISION,"/");
createTool(MulTool, MODE_MULTIPLICAITON,"*");
function createTool(mc:MovieClip, type:String, display:String){
mc.calcMode = type;
mc.modeDisplay = display;
mc.displayType.text = display;
mc.addEventListener(MouseEvent.CLICK, setModeMouseEvent);
}
function setModeMouseEvent(e:MouseEvent):void{
calcMode = e.currentTarget.calcMode;
modeDisplay = e.currentTarget.modeDisplay;
if(currentNum!="")
equalsMouseEvent();
totalDisplay.text = total.toString() + " " + modeDisplay ;
}
equTool.addEventListener(MouseEvent.CLICK, equalsMouseEvent);
function equalsMouseEvent(e:MouseEvent=null):void{
var curNum:int = parseInt(currentNum);
switch(calcMode){
case MODE_ADDITION:
total += curNum;
break;
case MODE_SUBTRACTION:
total -= curNum;
break;
case MODE_DIVISION:
total /= curNum;
break;
case MODE_MULTIPLICAITON:
total *= curNum;
break;
}
currentNum = "";
totalDisplay.text = total.toString();
}
See it in action here: http://www.filedropper.com/calcexample
I typically NEVER do any coding in the IDE--- usually always code in an editor, but because its for school im going to assume that they expect 'novice' work.
For instance, the buttons should be classed not just using the dynamic properties of MovieClip,... Also the buttons could have been drawn on via code--- but meh... for about 10 minutes it works pretty good me thinks...
Enjoy and best of luck! (if you want more advanced functions be sure to let me know, and if im bored I can give a crack at it... you said something about trig...)
ps.... blah... this is so gross looking noticed some of my variables are not typed... meh... just a general mock up..
pps. Yeah this thread is like 5 years old... way to go software studen....
~GK >^.^<
It is able to perform the calculations now but the problem i have now is the addDot function won't work. I have used two functions where i want to add a zero at the beginning of the dot when the display box is empty, however none of them seem to work. One method i have doesn't insert the zero before the decimal place however the other does add a zero but when i try to perform a calculation, it doesn't recognise the 0 as a decimal. For both the methods it also only adds a decimal for the first number but not the second number, however the first method i have calculates the numbers as a decimal but outputs as a whole number. So 6.2 + 2 = 64. Help would be greatly appreciatted, Thanks. Here is a link to download the calculator:
https://rapidshare.com/files/3385053...ulatornew2.fla