Skip to main content
Known Participant
March 14, 2012
Question

Calculator display

  • March 14, 2012
  • 1 reply
  • 969 views

I am working on building a calculator. following a tutorial on sayweb. I am trying to input the numbers into the display field using the following code:

import flash.events.MouseEvent;

var btn:Array = new Array();

for(var i = 0; i < 10; i++) {

          var myBtn:Btn = new Btn();

          myBtn.y = 15;

          myBtn.x = i * 100 + 15;

          myBtn.width = 48;

          myBtn.height = 48;

          myBtn.buttonMode = true;

          myBtn.mouseChildren = false;

          myBtn.caption.text = String (i);

          addChild(myBtn);

          btn.push(myBtn);

}

btn[0].y += 370;

btn[0].x += 10;

btn[1].y += 310;

btn[1].x += -90;

btn[2].y += 310;

btn[2].x += -130;

btn[3].y += 310;

btn[3].x += -170;

btn[4].y += 250;

btn[4].x += -390;

btn[5].y += 250;

btn[5].x += -430;

btn[6].y += 250;

btn[6].x += -470;

btn[7].y += 190;

btn[7].x += -690;

btn[8].y += 190;

btn[8].x += -730;

btn[9].y += 190;

btn[9].x += -770;

//OPERATORS//

var operators:Array = new Array();

for(var io:int = 0; io < 10; io++) {

          var opBtn:Btn_operator = new Btn_operator();

          opBtn.width = 48;

          opBtn.height = 48;

          opBtn.buttonMode = true;

          opBtn.mouseChildren = false;

          opBtn.caption_op.text = String (".");

          addChild(opBtn);

          operators.push(opBtn);

}

operators[0].y += 383;

operators[0].x += 78;

operators[0].caption_op.text = String(".");

operators[1].y += 383;

operators[1].x += 138;

operators[1].caption_op.text = String("=");

operators[2].y += 383;

operators[2].x += 198;

operators[2].caption_op.text = String("/");

operators[3].y += 323;

operators[3].x += 198;

operators[3].caption_op.text = String("*");

operators[4].y += 263;

operators[4].x += 198;

operators[4].caption_op.text = String("-");

operators[5].y += 203;

operators[5].x += 198;

operators[5].caption_op.text = String("+");

operators[6].y += 263;

operators[6].x += 256;

operators[6].caption_op.text = String("-/+");

operators[7].y += 323;

operators[7].x += 256;

operators[7].caption_op.text = String("C");

operators[8].y += 383;

operators[8].x += 256;

operators[8].caption_op.text = String("CE");

operators[9].y += 203;

operators[9].x += 256;

operators[9].caption_op.text = String("<-");

display_txt.text = "0";

var numberEntered:String ="";

//VARIABLE HANDLE OPERATION NOT BUTTON

var operate:String;

//HANDLE FIRST AND SECOND VALUE

var num1:Number;

var num2:Number;

//grouping all buttons in function

function addListeners():void{

          for (var n:uint=0; n < btn.length; n++){

                    btn.addEventListener(MouseEvent.CLICK,pressNumber);

          }

          for (n=0; n < operators.length; n++){

                    operators.addEventListener(MouseEvent.CLICK,pressOperator);

          }

          operators[8].addEventListener(MouseEvent.CLICK,clearAll);

          operators[0].addEventListener(MouseEvent.CLICK,addDot);

 

}

function pressNumber(event:MouseEvent):void{

          //FIND NAME OF BUTTON PRESSED

          var instanceName:String=event.target.name;

 

          var numPushed = instanceName.charAt(4);

 

if (display_txt.text == "0"){

          //REMOVE 0 FROM SCREEN

          display_txt.text ="";

}

          display_txt.appendText(numPushed);

}

 

function pressOperator(event:MouseEvent):void{}

function clearAll(event:MouseEvent):void{}

function addDot(event:MouseEvent):void{}

addListeners();

Here is the code im using to input:

function addListeners():void{

          for (var n:uint=0; n < btn.length; n++){

                    btn.addEventListener(MouseEvent.CLICK,pressNumber);

          }

          for (n=0; n < operators.length; n++){

                    operators.addEventListener(MouseEvent.CLICK,pressOperator);

          }

          operators[8].addEventListener(MouseEvent.CLICK,clearAll);

          operators[0].addEventListener(MouseEvent.CLICK,addDot);

 

}

function pressNumber(event:MouseEvent):void{

          //FIND NAME OF BUTTON PRESSED

          var instanceName:String=event.target.name;

 

          var numPushed = instanceName.charAt(4);

 

if (display_txt.text == "0"){

          //REMOVE 0 FROM SCREEN

          display_txt.text ="";

}

          display_txt.appendText(numPushed);

}

However in the display area it doesn't output the right numbers, but characters. so when charAt(4) it outputs 'a' and when its at charAt(3) it outputs 't'

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
March 14, 2012

You are not assigning names to your buttons, so it is outputting the default names that Flash applies which would be "instance"+some number... which is why you see t (3) and a (4).

You could just assign your buttons a number property when you create them and use that directly inst56ead of trying to deal with a string extraction...

for(var i = 0; i < 10; i++) {

          var myBtn:Btn = new Btn();

          myBtn.y = 15;

          myBtn.x = i * 100 + 15;

          myBtn.width = 48;

          myBtn.height = 48;

          myBtn.buttonMode = true;

          myBtn.mouseChildren = false;

          myBtn.num = Number(i);

          myBtn.caption.text = String (i);

          addChild(myBtn);

          btn.push(myBtn);

}

Then later on...

function pressNumber(event:MouseEvent):void{

      

       display_txt.appendText(event.currentTarget.num);

}

ay5haAuthor
Known Participant
March 14, 2012

I thought that may be the reason, Thank you. However now the other problem with the display i was having is the backspace button. I am using the slice function and instead of deleting the previous number it clears everything

//BACKSPACE DISPLAY AREA

operators[9].addEventListener(MouseEvent.CLICK, backSpace);

var numberEntered:String ="";

function backSpace(e:MouseEvent):void{

          numberEntered = numberEntered.slice(0,-1);

          display_txt.text = numberEntered;

}

Ned Murphy
Legend
March 14, 2012

Based on the code you show, numberEntered = "", so assigning any part of that to display_txt.text will still be "";