|
-
Not performing calculation
So i think i have fully coded my calculator but it still doesn't seem to be performing the calculations correctly. I can't see where i am going wrong. If i perform a calculation let's say 6*2 it outputs NaN then i straight away + by let's say 5 it outputs NaN again. And it doesn't make the operators display only once, so if i click on '+' twice it will display twice in the display when it is meant to only display once and in one of my functions i cleared the display field so that it would replace the number's when the next operation is performed but it doesn't seem to be doing that. I have been going over the code several times but i can't seem to identify where the problem is coming from.
Actionscript Code:
import flash.events.MouseEvent;
var btn:Array = new Array(); for(var i = 0; i < 10; i++) { var myBtn:Btn = new Btn(); myBtn.buttonMode = true; myBtn.mouseChildren = false; myBtn.num = Number(i); myBtn.caption.text = String (i); addChild(myBtn); btn.push(myBtn); btn[i].addEventListener(MouseEvent.CLICK,pressNumber);//EVENT TO ADD NUMBERS TO DISPLAY }
//OPERATORS// var operate:Array=new Array("÷","x","-","+"); var operators:Array = new Array(); for(var io:int = 0; io < 4; io++) { var opBtn:Btn_operator = new Btn_operator(); opBtn.buttonMode = true; opBtn.mouseChildren = false; opBtn.op = Number(operators); addChild(opBtn); operators.push(opBtn); opBtn.addEventListener(MouseEvent.CLICK, pressOperator);//EVENT TO ADD OPERATORS TO DISPLAY }
//EQUALS var equals:Btn_equals = new Btn_equals(); equals.buttonMode = true; equals.mouseChildren = false; equals.caption_op.text = String ("="); addChild(equals);
equals.addEventListener(MouseEvent.CLICK, performEquals);//EVENT TO CALCULATE
//VARIABLE HANDLE OPERATION NOT BUTTON var operates:String;
//HANDLE FIRST AND SECOND VALUE var num1:Number; var num2:Number;
var currentNumber:String =""; function pressNumber(e:MouseEvent):void{ display_txt.appendText(e.target.num); currentNumber+=(e.target.num); trace(e.target.num); }
function setNumber():void{ if (isNaN(num1)){ num1 = Number(currentNumber); } else { num2 = Number(currentNumber); } currentNumber = ""; }
//DISPLAY OPERATORS function pressOperator(event:MouseEvent):void{
operates = operate[operators.indexOf(event.target)]; display_txt.appendText(operate[operators.indexOf(event.target)]); trace(operate[operators.indexOf(event.target)]); setNumber(); //trace(operates); //CHECKING FOR VALUES AND STORING NUMBERS if(!(isNaN(num1)) || (isNaN(num2))){ performCalculation(); } }
function performCalculation():void{
switch (operate){ case "/": num1/=num2; break; case "x": num1*=num2; break; case "-": num1-=num2; break; case "+": num1+=num2; break; default: break; }
}
function performEquals(e:MouseEvent):void{ display_txt.text=String(num1); num2=NaN; performCalculation(); answer_txt.text=String(num2); trace(num2);
}
I have not added the full code as it is too long, i have just added the parts which relate to each other. I am just doing everything entirely through actionscript, nothing is on stage apart from the display and answer text fields. Help would be greatly appreciatted, thank you.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|