寒假无事,朋友要做个年终奖发奖用的抽奖摇号器。支持名字导入(用excel保存为123.xml文件,且在相同目录下xml文件夹内,123.xml中第一列为序号,第二列为姓名),显示,列出等功能。现将我做的一个版本发上来跟大家分享下~~
具体实现代码如下:
package classes { import flash.display.Sprite; import fl.controls.TextInput; import flash.text.TextFormat; import fl.controls.Button; import fl.controls.Label; import fl.controls.TextArea; import flash.events.MouseEvent; import flash.utils.Timer; import flash.events.TimerEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flashx.textLayout.formats.TextAlign; import flash.text.engine.TabAlignment; import flash.events.*; import flash.net.*; import flash.utils.*; import flash.sensors.Accelerometer; import fl.transitions.easing.Strong; import flash.net.*; public class yaohao extends Sprite { //================================================== private var okB:Button = new Button(); private var startB:Button =new Button(); private var stopB:Button= new Button(); private var outB:Button= new Button(); private var buttonStyle:TextFormat= new TextFormat(); //===================================================== private var showName:Label = new Label(); private var showNameStyle:TextFormat = new TextFormat(); //==================================== private var showNames:TextArea = new TextArea(); private var showNamesStyle:TextFormat = new TextFormat(); //======================================= private var originalLength:uint = new uint ;//数据导入时的初始长度namesArray //private var originalNumArray:Array = new Array(); //private var lastNumArray:Array = new Array(); //================================== private var timer:Timer; private var nameList:String; //================================== private var namesArray:Array=new Array();//二维数组【【ID】【name】;【】【】。。。】 private var lastNamesArray:Array = new Array(); private var jiazai:URLLoader=new URLLoader(); public function yaohao() { // constructor code initComponents(); } public function initComponents():void { this.input(); } //初始化个个器件。 private function input():void { //================================ this.buttonStyle.font = "微软雅黑"; this.buttonStyle.color = 0x000000; this.buttonStyle.size = 16; this.okB.label = "导入数据"; this.okB.move(10,114); this.okB.width = 104;//.setsize( , ); this.okB.height = 40; this.okB.setStyle("testFormat",this.buttonStyle); this.okB.addEventListener(MouseEvent.CLICK,inputNum); stage.addChild(okB); //===============================加入showName组件。 this.showNameStyle.font = "微软雅黑"; this.showNameStyle.color = 0xFFFFFF; this.showNameStyle.size = 16; this.showName.move(327.85,353.9); this.showName.setSize(104,39.95); this.showName.autoSize = TextFieldAutoSize.CENTER; this.showName.setStyle("textFormat",this.showNameStyle); this.showName.text = "开始摇奖吧!"; stage.addChild(this.showName); //==============================创建开始按钮。 this.startB.label = "开始摇奖"; this.startB.move(247.9,497.85); this.startB.setSize(104,40); this.startB.setStyle("textFormat",this.buttonStyle); this.startB.addEventListener(MouseEvent.CLICK,startNumArrayShow); stage.addChild(this.startB); //============================= this.stopB.label = "得奖"; this.stopB.move(395.85,495.85); this.stopB.setSize(104,40); this.stopB.setStyle("textFormat",this.buttonStyle); this.stopB.addEventListener(MouseEvent.CLICK,getNamesArray); stage.addChild(this.stopB); //=================================== //this.showNames=new TextArea(); this.showNamesStyle.font = "微软雅黑"; this.showNamesStyle.size = 14; this.showNamesStyle.color = 0x000000; this.showNames.x = 717.75; this.showNames.y = 16; this.showNames.width = 113.95; this.showNames.height = 149.95; this.showNames.textField.height = 149.95; this.showNames.textField.width = 113.95; this.showNames.alpha = 1; this.showNames.setStyle("textFormat",this.showNamesStyle); this.showNames.wordWrap = true; nameList = "--抽奖结果--\n"; this.showNames.text = nameList; stage.addChild(this.showNames); //=============== this.outB.label = "导出名单"; this.outB.move(727.95,175); this.outB.setSize(104,40); this.outB.setStyle("textFormat",this.buttonStyle); this.outB.addEventListener(MouseEvent.CLICK,exportData); stage.addChild(this.outB); //========================== var delay:Number = 10; var repeat:int = 0; this.timer = new Timer(delay,repeat); //================ this.startB.enabled = false; this.stopB.enabled = false; } //初始化器件结束 private function inputNum(eve:MouseEvent):void { jiazai.load(new URLRequest("xml/123.xml")); jiazai.addEventListener(Event.COMPLETE,wanCheng); this.okB.enabled = false; } private function wanCheng(evt:Event):void { var a:XML = new XML(jiazai.data); trace(a.children().length()); var cells:XMLList = a.children()[4].children()[0].children(); for each (var cell:XML in cells) { var namesList:Array = [cell.children()[0].children(),cell.children()[1].children()]; namesArray.push(namesList); } trace(namesArray); trace(namesArray.length); this.originalLength = namesArray.length;//初始化 原始长度 if (namesArray.length > 0) { this.startB.enabled = true; } this.jiazai = null; } private function startNumArrayShow(eve:MouseEvent):void { //trace("here"); if (this.namesArray.length > 1) { if (this.startB.enabled) { this.startB.enabled = false; timer.addEventListener(TimerEvent.TIMER,showNamesArray); //时间控制方法。; timer.start(); this.stopB.enabled = true; } else { } } else { this.startB.enabled = false; this.stopB.enabled = false; this.showName.text = "抽奖完毕"; this.okB.enabled = false; } } //时间控制方法。 private function showNamesArray(even:TimerEvent):void { //trace("here?"); var i:uint=uint(Math.round(Math.random() * (this.namesArray.length-1))); var j:uint = 1; if (i != 0) { this.showName.text = namesArray[i][j].toString(); trace(i+" | "+j); } else { } } // 获奖按钮的实现方法=========================== var order:uint = 0; private function getNamesArray(eve:MouseEvent):void { timer.stop(); if (this.namesArray.length > 1) { this.stopB.enabled = false; this.startB.enabled = true; this.okB.enabled = false; //breakout:while (true) //{ //************************************************************************** var getTheNameKey:String=this.namesArray[1+Math.round(Math.random()*(this.namesArray.length-2))][0]; //trace(typeof(getTheNameKey)+ " : "+getTheNameKey); //trace(typeof(this.namesArray[i][0].toString())+ " : "+this.namesArray[i][0]); for(var i:int=0;i<=this.namesArray.length;i++) { //trace(typeof(getTheNameKey)+ " : "+getTheNameKey); //trace(typeof(this.namesArray[i][0].toString())+ " : "+this.namesArray[i][0]); if (getTheNameKey == this.namesArray[i][0].toString()) { trace(typeof(this.namesArray[i][0].toString())+ " : "+this.namesArray[i][0]); trace(typeof(getTheNameKey)+ " : "+getTheNameKey); order++; var theName:String = this.namesArray[i][1]; var nameRow:Array = [String("第 " + order + " 个是: "),this.namesArray[i][0],this.namesArray[i][1]]; this.lastNamesArray.push(nameRow); this.namesArray.splice(i,1); this.showName.text = String(theName); this.nameList += "第" + order + "名 : " + theName + " \n"; //trace("?"); break; //break breakout; } else { } } //getTheNameKey=""; //} } else { this.stopB.enabled = false; this.startB.enabled = false; this.showName.text = "抽奖完毕"; this.okB.enabled = false; } //trace("??"); this.showNames.text = this.nameList; trace(showNames.text); trace(this.lastNamesArray); trace(this.lastNamesArray.length); } private function exportData(eve:MouseEvent):void { var resultText:String=""; for each( var e:String in this.lastNamesArray) { resultText +=e+"\t\t\n"; } trace(resultText); var file:FileReference = new FileReference; file.save(resultText,"results.txt"); } } }