3 Replies Latest reply on Feb 28, 2014 4:29 AM by ASilva

    Calculator - Flash help needed in code


      hi there i am doing a work for school and i came up with a problem


      i currently have this code:


      somar.addEventListener(MouseEvent.CLICK, adicionar);
      Subtrair.addEventListener(MouseEvent.CLICK, retirar);
      multiplicar.addEventListener(MouseEvent.CLICK, multiplicacao);
      Dividir.addEventListener(MouseEvent.CLICK, divisao);
      function adicionar (MouseEvent){
                var Resultado: Number = Number (caixa1.text) + Number (caixa2.text);
                caixa3.text = String (Resultado);
      function retirar (MouseEvent){
                var Resultado: Number = Number (caixa1.text) + Number (caixa2.text);
                caixa3.text = String (Resultado);
      function divisao (MouseEvent){
                var Resultado: Number = Number (caixa1.text) + Number (caixa2.text);
                caixa3.text = String (Resultado);
      function multiplicacao (MouseEvent){
                var Resultado: Number = Number (caixa1.text) + Number (caixa2.text);
                caixa3.text = String (Resultado);


      in short i only can do basic operations like "+", "-", "x" and "/" .... and basically this code above ... will allow me to write the number on the test box 1 and 2 and then i click either one of the operations and the result shows on the last text box.


      but ... now comes the tricky part for me, i want to complement my calculator by saying the errors, for example ... when we write on the text box a character and a number, the calculator should show error, and so on, i also wanted to had a button to clear the calculator ... but i have no clue how to do it


      i first tried doing a "if else" sequence, but i ended up deleting the code by mistake ... it wasn't working in the first place.


      So if there is someone that can help me i would appreciate it



      Regards ASilva

        • 1. Re: Calculator - Flash help needed in code
          moccamaximum Level 5

          you can test any object for class-membership by using sth. like


          var a:Number = 5;

          var b:String = "5";

          var c:String = "a";

          trace(a is Number);


          trace(b is Number);




          //meaning: Not a number

          //use this to check if the input String is feasible for arihmethic manipulation

          1 person found this helpful
          • 2. Re: Calculator - Flash help needed in code
            ASilva Level 1

            humm ... i think i understand what you mean ... but wont that only work when i put the number 5 and leter a ??


            At least that is what ... i am understanding.


            BTW is it possible to reedit the post above ?? i just realized a mistake i made ... i wrote "+" in all operation

            • 3. Re: Calculator - Flash help needed in code
              ASilva Level 1

              Hi there ... sorry for making this discussion ... but i already managed with the help of my teacher create a code for what i was asking.


              Here it is:



              somar.addEventListener(MouseEvent.CLICK, cliqbot);
              subtrair.addEventListener(MouseEvent.CLICK, cliqbot);
              multiplicar.addEventListener(MouseEvent.CLICK, cliqbot);
              dividir.addEventListener(MouseEvent.CLICK, cliqbot);
              var counter:Number = 1;
              function cliqbot (event:MouseEvent): void{
                        if(event.target==somar) {caixa_sinais.text="+"; counter=1;}
                        if(event.target==subtrair){caixa_sinais.text="-"; counter=2;}
                        if(event.target==multiplicar){caixa_sinais.text="*"; counter=3;}
                        if(event.target==dividir){caixa_sinais.text="/"; counter=4;}
              function limpar (event:MouseEvent){
              function resultado (event:MouseEvent):void{
                  if (caixa1.text=="" || caixa2.text==""|| isNaN(Number(caixa1.text))||isNaN(Number(caixa1.text))) 
                                  switch (counter){
                                            case 1:
                                                      var resultado1:Number = Number (caixa1.text) + Number(caixa2.text);
                                            case 2:
                                                      var resultado2:Number = Number (caixa1.text) - Number(caixa2.text);
                                            case 3:
                                                      var resultado3:Number = Number (caixa1.text)* Number(caixa2.text);
                                            case 4:
                                                      if (Number(caixa2.text)==0){output.text="Erro!!"}
                                                      var resultado4:Number = Number (caixa1.text) / Number(caixa2.text);


              Well i guess this is one way of doing it right ???


              is there a way to do it with the "if else" sequence ??