As3 简单计算器的例子

package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import fl.controls.Button;

	/*
	在flash builder里,默认会生成一个全屏、背景色为#869CA7
	、帧数为24/秒的swf文件,要修改这些参数,只需要在类文件中定义 
	[SWF(width="800", height="600", backgroundColor="#ffffff", frameRate="31")]
	*/
//	[SWF(width="320", height="260", backgroundColor="#fffffw", frameRate="31")]
	[SWF(width="210", height="200", backgroundColor="#ffffff", frameRate="31")]
	public class Test_As3 extends Sprite
	{
		
		private var _text:TextField;
		public function Test_As3(){
			
			this.mouseChildren = true;
			init_text();
			initButton();
			this.addEventListener(MouseEvent.CLICK, getClickValue);
		}
		
		private var a1:String="";
		private var a2:String="";
		private var aChat:String;
		private var nbo:Boolean=false;
		
		private function getClickValue(evt:MouseEvent):void{
			trace(evt.target.label);
			if(evt.target.label=="*"||evt.target.label=="/"
				||evt.target.label=="+"||evt.target.label=="-"){
				a1=_text.text;
				aChat=evt.target.label;
				nbo=true;
				_text.text="";
			}else if(evt.target.label!="="){
				if(nbo==true){
					a2 =_text.text+evt.target.label;
					_text.text=a2;
				}else{
					a1=_text.text+evt.target.label;
					_text.text=a1;
				}
			}
			
			if(evt.target.label=="清除"){
				a1="";
				a2="";
				_text.text="";
			}
			
			if(evt.target.label=="="){
				trace(aChat);
				switch(aChat) {
					case "+":
						_text.text=String(int(a1)+int(a2));
						break;
					case "-":
						_text.text=String(int(a1)-int(a2));
						break;
					case "*":
						_text.text=String(int(a1)*int(a2));
						break;
					case "/":
						_text.text=String(int(a1)/int(a2));
						break;
				}
			}
		}
		
		
		private function init_text():void{
			_text = new TextField();
			_text.border = true;
			_text.borderColor = 0x0;
			_text.width = 209;
			_text.height = 40;
			this.addChild(_text);
		}
		
		private var _button:Button;
		private var moveX:uint = 10, moveY:uint = 50;
		private function initButton():void{
			
			var array:Array = new Array();
			array.push(["+","-","*","/"]);
			array.push(["1","2","3","清除"]);
			array.push(["4","5","6","="]);
			array.push(["7","8","9","0"]);
			for(var i:uint = 0; i < array.length; i++){
				for(var j:uint = 0; j < array[i].length; j++){
					_button = new Button();
					_button.label = array[i][j];
					_button.width = 40;
					_button.height = 20;
					_button.move(moveX, moveY);
					this.addChild(_button);
					moveX += 50;
				}
				moveY += 30;
				moveX = 10;
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值