-
ActionScript 3.0 Simple Calculator
Hi there,
I need a little help from you guys..
I've already done mostly all the necessary part but I'm not able to handle the 'Calculation part'
The thing is that the calculations are done in a single text field.. It wouldn't be a problem if it was in 2 txt fields..
So please help me on this one --> Only the calculation part.. even for the "+" operator..then i'll do the rest
I'll attach a file for more info
Thank You
calculator.fla
MyCalc.png
-
Senior Member
Lol, this is not your file You do not realize it works perfectly, in particular the "+" operator.
-
I know
But I'm not able to get the answer in the result_txt.text
The Display_txt is fine apart that that i can only insert only 1 decimal place only 1 time
Plz do the solution for me
-
Dignitary
Originally Posted by LuffyMcAwesome
Plz do the solution for me
Now Luffy, my friend, if I did that how would you learn anything?
-
Well.... umm
I'll try to understand the logic....and then re-factor it
Plz help me.. I'm getting a feeling that its only with a few lines of codes but i'm not able to work it out
-
Dignitary
Well, let me give you some advice. If this is for a school assignment, which is the only reason people build calculators, I would definitely NOT use this file. The code is a sloppy mix of old and new actionscript. Particularly the use of the onPress and text variables.
This assignment can be made very simple by using proper as3 methods and simple logic.
For instance, use event listeners for each button and call on one function instead of turning every button press into a separate function.
There is no need to convert anything into a string + - * / = are all math operators.
The point (.) fits into a number type variable, no need to take any extra steps to handle that.
-
Thanks... Just one last thing
How to do the calculation of the 2 numbers in the same textfield?? Do you store them in a variable or something?
If there was 2 txt_fields, this thread would've never existed
I was thinking maybe the 2 values should be dealt in the maths operators function
Any tips or sample code how to calculate the 2 numbers in the same field
Hope you don't mind
-
Dignitary
Originally Posted by LuffyMcAwesome
Thanks... Just one last thing
How to do the calculation of the 2 numbers in the same textfield?? Do you store them in a variable or something?
If there was 2 txt_fields, this thread would've never existed
I was thinking maybe the 2 values should be dealt in the maths operators function
Any tips or sample code how to calculate the 2 numbers in the same field
Hope you don't mind
OK, see... now you are thinking about it and not just asking for an answer. I like that a lot!
Ima download this now and make it work, hope you have flash CS6.
-
Yeah i've got CS6
Thanks Rynoe
-
Dignitary
The upload seems to be broken so here is the code:
PHP Code:
import flash.events.MouseEvent;
var currentNumber:Number = 0; var memoryNumber:String=""; var operator:String = "";
input_0.addEventListener(MouseEvent.CLICK, numberPushed); input_1.addEventListener(MouseEvent.CLICK, numberPushed); input_2.addEventListener(MouseEvent.CLICK, numberPushed); input_3.addEventListener(MouseEvent.CLICK, numberPushed); input_4.addEventListener(MouseEvent.CLICK, numberPushed); input_5.addEventListener(MouseEvent.CLICK, numberPushed); input_6.addEventListener(MouseEvent.CLICK, numberPushed); input_7.addEventListener(MouseEvent.CLICK, numberPushed); input_8.addEventListener(MouseEvent.CLICK, numberPushed); input_9.addEventListener(MouseEvent.CLICK, numberPushed); action_point.addEventListener(MouseEvent.CLICK, buttonPushed); action_equals.addEventListener(MouseEvent.CLICK, buttonPushed); action_divide.addEventListener(MouseEvent.CLICK, buttonPushed); action_multiply.addEventListener(MouseEvent.CLICK, buttonPushed); action_subtract.addEventListener(MouseEvent.CLICK, buttonPushed); action_add.addEventListener(MouseEvent.CLICK, buttonPushed); action_clear.addEventListener(MouseEvent.CLICK, buttonPushed); action_clearall.addEventListener(MouseEvent.CLICK, buttonPushed);
function sum(operator):void{ var total:Number switch(operator){ case "-": total = Number(memoryNumber) - Number(currentNumber); break; case "+": total = Number(memoryNumber) + Number(currentNumber); break; case "/": total = Number(memoryNumber) / Number(currentNumber); break; case "*": total = Number(memoryNumber) * Number(currentNumber); break; } displayTxt.text = total; currentNumber = Number(displayTxt.text); operator="" memoryNumber="" } function buttonPushed(m:MouseEvent):void{ if(m.currentTarget.name=="action_point"){ displayTxt.appendText(".") action_point.removeEventListener(MouseEvent.CLICK, buttonPushed); }else if(m.currentTarget.name=="action_equals"){ if(operator!=""){ sum(operator) } }else{ var whichOne:String = m.currentTarget.name; memoryNumber=currentNumber; displayTxt.text = ""; switch(whichOne){ case "action_divide": operator="/" break; case "action_multiply": operator="*" break; case "action_subtract": operator="-" break; case "action_add": operator="+" break; case "action_clear": currentNumber= 0; break; case "action_clearall": currentNumber= 0; memoryNumber=""; break; } } } function numberPushed(m:MouseEvent):void{ if(m.currentTarget.name.slice(-1)==0){ if(currentNumber>0 ){ displayTxt.appendText(m.currentTarget.name.slice(-1)); } }else{ displayTxt.appendText(m.currentTarget.name.slice(-1)); } currentNumber = Number(displayTxt.text); }
Name the textfield "displayTxt". Variables objects for textfields no longer exist in as3.
Last edited by rynoe; 03-25-2013 at 01:06 PM.
[SIGPIC][/SIGPIC]
-
Thanks It Worked...
Along with some small modification from my side
-
Dignitary
Well, I hope you learned something. I didn't want to just give you the answer.
Edit: BTW: for anyone that sees this. The '+' operator also is the concatonate operator for strings. This can create bugs.
Last edited by rynoe; 03-26-2013 at 12:36 AM.
[SIGPIC][/SIGPIC]
-
Yeah... I managed to show the full calculation (workings) in the displayTxt field
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
|