A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: Need a Calculator in AS3 please...

Hybrid View

  1. #1
    Ө_ө 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;
    ... 

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

  3. #3
    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?

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