ActionScript 3.0 Simple Calculator
A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: ActionScript 3.0 Simple Calculator

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9

    Lightbulb 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

  2. #2
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    663
    Lol, this is not your file You do not realize it works perfectly, in particular the "+" operator.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    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

  4. #4
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    756
    Quote Originally Posted by LuffyMcAwesome View Post
    Plz do the solution for me
    Now Luffy, my friend, if I did that how would you learn anything?

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    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

  6. #6
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    756
    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.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    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

  8. #8
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    756
    Quote Originally Posted by LuffyMcAwesome View Post
    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.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Yeah i've got CS6

    Thanks Rynoe

  10. #10
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    756
    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.CLICKnumberPushed);
    input_1.addEventListener(MouseEvent.CLICKnumberPushed);
    input_2.addEventListener(MouseEvent.CLICKnumberPushed);
    input_3.addEventListener(MouseEvent.CLICKnumberPushed);
    input_4.addEventListener(MouseEvent.CLICKnumberPushed);
    input_5.addEventListener(MouseEvent.CLICKnumberPushed);
    input_6.addEventListener(MouseEvent.CLICKnumberPushed);
    input_7.addEventListener(MouseEvent.CLICKnumberPushed);
    input_8.addEventListener(MouseEvent.CLICKnumberPushed);
    input_9.addEventListener(MouseEvent.CLICKnumberPushed);
    action_point.addEventListener(MouseEvent.CLICKbuttonPushed);
    action_equals.addEventListener(MouseEvent.CLICKbuttonPushed);
    action_divide.addEventListener(MouseEvent.CLICKbuttonPushed);
    action_multiply.addEventListener(MouseEvent.CLICKbuttonPushed);
    action_subtract.addEventListener(MouseEvent.CLICKbuttonPushed);
    action_add.addEventListener(MouseEvent.CLICKbuttonPushed);
    action_clear.addEventListener(MouseEvent.CLICKbuttonPushed);
    action_clearall.addEventListener(MouseEvent.CLICKbuttonPushed);

    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.CLICKbuttonPushed);
        }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":
                
    currentNumber0;
                break;
                case 
    "action_clearall":
                
    currentNumber0;
                
    memoryNumber="";
                break;
            }
        }
    }
    function 
    numberPushed(m:MouseEvent):void{
        if(
    m.currentTarget.name.slice(-1)==0){
            if(
    currentNumber>){
                
    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.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks It Worked...

    Along with some small modification from my side

  12. #12
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    756
    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.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    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
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com