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;
}
}
}
}
As3 简单计算器的例子
最新推荐文章于 2021-04-19 15:27:39 发布