A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: Need a Calculator in AS3 please...

  1. #1
    Junior Member
    Join Date
    Sep 2006
    Posts
    15

    Need a Calculator in AS3 please...

    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?

  2. #2
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    What exactly do you need help with?

    Sprites? Event handling? Classes? Packages?

  3. #3
    Junior Member
    Join Date
    Sep 2006
    Posts
    15
    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...

  4. #4
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    PM sent

  5. #5
    Junior Member
    Join Date
    Feb 2008
    Posts
    3

    Need That Calculator, Too!

    Hi Afternoon Delight,
    Would you please send me a copy of that calculator code, too? I have a similar situation.
    Much appreciated,
    phoolholy

  6. #6
    indime
    Join Date
    Feb 2008
    Location
    Manchester, England
    Posts
    2
    Quote Originally Posted by phoolholy
    Hi Afternoon Delight,
    Would you please send me a copy of that calculator code, too? I have a similar situation.
    Much appreciated,
    phoolholy
    Hi
    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

  7. #7
    Junior Member
    Join Date
    Feb 2008
    Posts
    3

    Calculator code

    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;
    }
    }

  8. #8
    indime
    Join Date
    Feb 2008
    Location
    Manchester, England
    Posts
    2

    Calculator

    Thanks

    I will use this to build on. I will try and add trig functiions, will post when done.

    Cheers

    indime

  9. #9
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    @ 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...



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  10. #10
    Junior Member
    Join Date
    Feb 2008
    Posts
    3

    Yeah

    This is code my teacher gave me. Not the best, I know.
    Thanks.

  11. #11
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    ^ 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.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  12. #12
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    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...

  13. #13
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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;
    ... 

  14. #14
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    I mean if you had to have the buttons onscreen before and you couldn't generate them with code --

    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.
           }
        }
    }
    }
    I just realized that won't work 'cause the operations are retroactive...uh...you'd just have to queue the last operation.
    Whatever.
    Point is use a loop to add your buttons and events
    Last edited by joshstrike; 02-06-2008 at 07:52 PM.

  15. #15
    Junior Member
    Join Date
    Sep 2009
    Posts
    1

    reuse answer

    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?

  16. #16
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    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.

  17. #17
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    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.

  18. #18
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    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//*

  19. #19
    Senior Member guardiankitty's Avatar
    Join Date
    Dec 2006
    Location
    Here
    Posts
    215
    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 >^.^<
    Last edited by guardiankitty; 03-21-2012 at 03:44 AM.

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

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