0 Replies Latest reply on Jan 2, 2012 3:26 AM by xllopes

    Number display in a calculator

    xllopes

      Hi,

       

      I am new to actionscript and Flash, and I have been doing a tutorial on how to build a calculator.

       

      It all seems to be working fine, except that the numbers aren't showing in the display window, well any number except for "0", zero is showing just fine.

       

      This is the code I have so far (following the tutorial):

       

      ---------------------------------------------------------

       

      var numbers:Array = [num0Btn,num1Btn,num2Btn,num3Btn,num4Btn,num5Btn,num6Btn,num7Btn,num8Btn,num9Btn];

      var operators:Array = [divideBtn,addBtn,subtractBtn,multiplyBtn,equalBtn];

      var op:String;

      var num1:Number;

      var num2:Number;

       

      function addListeners():void

      {

          for(var i:uint = 0; i < numbers.length; i++)

          {

              numbers[i].addEventListener(MouseEvent.CLICK, pressNumber);

          }

          for(i = 0; i < operators.length; i++)

          {

              operators[i].addEventListener(MouseEvent.CLICK, pressOperator);

          }

          clearBtn.addEventListener(MouseEvent.CLICK, clearAll);

          dotBtn.addEventListener(MouseEvent.CLICK, addDot);

      }

       

      function pressNumber(event:MouseEvent):void

      {

          var instanceName:String = event.target.name;

          var numPushed:String = instanceName.charAt(3);

          trace(instanceName);

          trace(numPushed);

          trace(windowTxt);

          var texto:String = windowTxt.text;

          trace(texto);

          if(windowTxt.text == "0")

          {

              windowTxt.text = "";

          }

          windowTxt.appendText(numPushed);

          trace(windowTxt.text);

      }

       

      function pressOperator(event:MouseEvent):void

      {

          var instanceName:String = event.target.name;

          var currentOp:String = instanceName.slice(0, instanceName.indexOf("B"));

          trace(currentOp);

      }

       

      function clearAll(event:MouseEvent):void

      {

          windowTxt.text = "0";

          num1 = NaN;

          num2 = NaN;

      }

       

      function addDot(event:MouseEvent):void

      {

          if(windowTxt.text.indexOf(".") == -1)

          {

              windowTxt.appendText(".");

          }

      }

       

      addListeners();

       

      ----------------------------------------------------------------

       

      For example I have tried out the calculator, inserting some numbers and this is part of what is traced:

       

      6

      [object TextField]

      12036803259

      120368032596

      num3Btn

      3

      [object TextField]

      120368032596

      1203680325963

      num0Btn

      0

      [object TextField]

      1203680325963

      12036803259630

       

      so I know this is working, the only problem is that at this point point the only thing in the display window is "000".

       

      The properties of windowTxt are: Classic Text and Dynamic Text

      I have tried it with different fonts, but still isn't working

       

      Thanks in advance for any help you can provide me.

       

      Best regards