5 Replies - 5798 Views - Last Post: 16 March 2012 - 03:52 PM

#1 adamkay123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-October 11

limit numbers in flash actionscript 3 calculator!

Posted 16 March 2012 - 07:46 AM

hello everyone im making a calculator and done everything one last thing and that limit the amount of numbers in the display screen to 10 this is my code so far and was wondering if theres any help it would be appreciated ive been banging my head against the wall and getting nowhere...

var numbers:Array = [num0_btn,num1_btn,num2_btn,num3_btn,num4_btn,num5_btn,num6_btn,num7_btn,num8_btn,num9_btn];
var operators:Array = [divide_btn,add_btn,subtract_btn,multiply_btn,equal_btn];
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);
	}
	clear_btn.addEventListener(MouseEvent.CLICK, clearAll);
	dot_btn.addEventListener(MouseEvent.CLICK, addDot);
	back_btn.addEventListener(MouseEvent.CLICK, backSpace);
	
}

function pressNumber(event:MouseEvent):void
{
	var instanceName:String = event.target.name;
	var numPushed:String = instanceName.charAt(3);
	if(window_txt.text == "0" || num1 == Number(window_txt.text))
	{
		window_txt.text = "";
	}
	window_txt.appendText(numPushed);
}

function pressOperator(event:MouseEvent):void
{
	var instanceName:String = event.target.name;
	var currentOp:String = instanceName.slice(0, instanceName.indexOf("_"));
	if(!num1)
	{
		num1 = Number(window_txt.text);
		op = currentOp;
	}
		
	else if(!num2)
	{
		num2 = Number(window_txt.text);
		showCalculation();
		op = currentOp;
	}
	
}

function clearAll(event:MouseEvent):void
{
	window_txt.text = "0";
	num1 = NaN;
	num2 = NaN;
}

function addDot(event:MouseEvent):void
{
	if(num1 == Number(window_txt.text))
	{
		window_txt.text = "0";
	}
	if(window_txt.text.indexOf(".") == -1)
	{
		window_txt.appendText(".");
	}
}

function backSpace(event:MouseEvent):void
{
      
    window_txt.text = window_txt.text.substring(0, window_txt.text.length-1);

}

function showCalculation():void
{
var dividedZero:Boolean;
switch(op)
{
case "multiply" :
num1 *= num2;
break;
case "divide" :
if(num2 != 0) {
    num1 /= num2;
}else{
     window_txt.text = "cannot divide by zero";
     dividedZero = true;
}
break;
case "add" :
num1 += num2;
break;
case "subtract" :
num1 -= num2;
break;
default :
break;
}
if(!dividedZero){
   window_txt.text = String(num1);
}
num2 = NaN;
}


addListeners();




Is This A Good Question/Topic? 0
  • +

Replies To: limit numbers in flash actionscript 3 calculator!

#2 adamkay123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-October 11

Re: limit numbers in flash actionscript 3 calculator!

Posted 16 March 2012 - 08:39 AM

right ive got to a point where i know i need to use [as]window_txt.maxChars = 10;[/as]

but i dont know where i need to put that ive tried it near the vars but that didnt work any ideas??
Was This Post Helpful? 0
  • +
  • -

#3 adamkay123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-October 11

Re: limit numbers in flash actionscript 3 calculator!

Posted 16 March 2012 - 09:30 AM


function pressNumber(event:MouseEvent):void
{
	window_txt.maxChars =  10;
	var instanceName:String = event.target.name;
	var numPushed:String = instanceName.charAt(3);
	if(window_txt.text == "0" || num1 == Number(window_txt.text))
	{
		window_txt.text = "";
	}
	window_txt.appendText(numPushed);
	
}



i thought this is where it goes because this is where you enter the numbers but it dont work?? any ideas
Was This Post Helpful? 0
  • +
  • -

#4 adamkay123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-October 11

Re: limit numbers in flash actionscript 3 calculator!

Posted 16 March 2012 - 10:21 AM

i changed the maxchars in textfield properties and still no luck
Was This Post Helpful? 0
  • +
  • -

#5 adamkay123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-October 11

Re: limit numbers in flash actionscript 3 calculator!

Posted 16 March 2012 - 10:50 AM

i give up ive been trying all day if no one wants to give me this small code that could work then fine im just going to leave it now
Was This Post Helpful? 0
  • +
  • -

#6 GreenSpiral  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 30-December 08

Re: limit numbers in flash actionscript 3 calculator!

Posted 16 March 2012 - 03:52 PM

If you had read the documentation for the maxChars property you would have found this:

Quote

The maximum number of characters that the text field can contain, as entered by a user. A script can insert more text than maxChars allows; the maxChars property indicates only how much text a user can enter. If the value of this property is 0, a user can enter an unlimited amount of text.

The default value is 0.

This post has been edited by GreenSpiral: 16 March 2012 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1