文档类Main.as
0001 package { 0002 import flash.display.Sprite; 0003 import flash.display.MovieClip; 0004 import flash.events.Event; 0005 import flash.events.MouseEvent; 0006 0007 import flash.display.SimpleButton; 0008 import flash.text.TextField; 0009 import flash.text.TextFieldAutoSize; 0010 import flash.utils.ByteArray; 0011 0012 import lu.leon.utils.DataProxy; 0013 import lu.leon.display.WinnersBrowser; 0014 import lu.leon.display.WinnerItem; 0015 import lu.leon.display.ResultPanel; 0016 0017 import com.greensock.TweenLite; 0018 import com.greensock.easing.*; 0019 0020 /** 0021 * @author LeonLu 0022 * @email [email protected] 0023 * @date 2010-12-14 9:38 0024 **/ 0025 //[SWF(width="",height="",frameRate="",backgroundColor="#")] 0026 public class Main extends Sprite { 0027 private var guestsXmlPath:String = "xml/guests.xml"; 0028 private var guestsXmlLoader:DataProxy; 0029 private var sum:int; 0030 0031 private var winnersBrowser:WinnersBrowser; 0032 private var resultPanel:ResultPanel; 0033 private var resultWinnerItemsContainer:Sprite; 0034 0035 private var resultBtnCount:int = 0; 0036 0037 public function Main():void { 0038 if (stage) 0039 init(); 0040 else 0041 addEventListener(Event.ADDED_TO_STAGE, init); 0042 } 0043 0044 private function init(e:Event = null):void { 0045 removeEventListener(Event.ADDED_TO_STAGE, init); 0046 0047 loadGuestsXml(); 0048 } 0049 0050 private function loadGuestsXml():void { 0051 guestsXmlLoader = new DataProxy(guestsXmlPath); 0052 guestsXmlLoader.addEventListener(DataProxy.XML_LOADED, guestsXmlLoaded, false, 0, true); 0053 } 0054 0055 private function guestsXmlLoaded(e:Event):void { 0056 GobalData.guestsXml = guestsXmlLoader.xml; 0057 sum = GobalData.guestsXml.guest.length(); 0058 for (var i:int = 0; i < sum; i++){ 0059 var id:int = GobalData.guestsXml.guest[i].@id; 0060 GobalData.ary_originalIds[i] = id; 0061 } 0062 0063 GobalData.ary_currentIds = cloneObject(GobalData.ary_originalIds); 0064 0065 addWinnersBrowser();//新建抽奖类 0066 0067 addResultPanel();//新建结果面板 0068 resultBtn.addEventListener(MouseEvent.CLICK, resultBtnClicked, false, 0, true); 0069 0070 startStopBtn.buttonMode = true; 0071 startStopBtn.addEventListener(MouseEvent.CLICK, startStopBtnClicked, false, 0, true); 0072 0073 resetBtn.mouseEnabled = false;//开始没有抽奖时,重置按钮不可用 0074 resetBtn.addEventListener(MouseEvent.CLICK, resetBtnClicked, false, 0, true); 0075 } 0076 0077 private function addWinnersBrowser():void { 0078 winnersBrowser = new WinnersBrowser(); 0079 addChild(winnersBrowser); 0080 winnersBrowser.setData(winnersNumMc.tf_num.text); 0081 } 0082 0083 private function startStopBtnClicked(e:MouseEvent):void { 0084 switch (startStopBtn.currentFrame){ 0085 case 1: 0086 //只有当中奖名额>0时,才能抽奖 0087 if (int(winnersNumMc.tf_num.text) > 0){ 0088 startStopBtn.gotoAndPlay(2); 0089 //删除上次抽奖结果winnerItems,新建一批winnerItems 0090 winnersBrowser.refreshWinnerItems(winnersNumMc.tf_num.text); 0091 winnersBrowser.startDrawing(); 0092 resetBtn.mouseEnabled = false;//抽奖进行时,重置按钮不可用 0093 } 0094 break; 0095 0096 case 2: 0097 startStopBtn.gotoAndPlay(1); 0098 winnersBrowser.stopDrawing(); 0099 refreshResultPanel();//刷新结果面板 0100 resetBtn.mouseEnabled = true;//抽奖结束后,重置按钮才可用 0101 break; 0102 } 0103 } 0104 0105 private function refreshResultPanel():void { 0106 removeResultPanelChildren();//删除上次抽奖结果 0107 creatResultPanelItems();//创建新的抽奖结果(其实是重新for循环排列) 0108 } 0109 0110 //============================================================ 0111 private function creatResultPanelItems():void { 0112 var row:int = 0; 0113 var col:int = 0; 0114 for (var i:int = 0; i < GobalData.ary_winnerIds.length; i++){ 0115 var winnerItem:WinnerItem = new WinnerItem(); 0116 var id:int = GobalData.ary_winnerIds[i]; 0117 var _name:String = GobalData.guestsXml.guest.(@id == id).@name; 0118 winnerItem.setData(_name); 0119 resultWinnerItemsContainer.addChild(winnerItem); 0120 winnerItem.x = (winnerItem.width + 30) * col; 0121 winnerItem.y = (winnerItem.height + 20) * row; 0122 0123 col++; 0124 if (col == 5){ 0125 row++; 0126 col = 0; 0127 } 0128 } 0129 } 0130 0131 private function removeResultPanelChildren():void { 0132 var childNum:int = resultWinnerItemsContainer.numChildren; 0133 if (childNum > 0){ 0134 for (var i:int = 0; i < childNum; i++){ 0135 var winnerItem:WinnerItem = resultWinnerItemsContainer.getChildAt(0) as WinnerItem; 0136 resultWinnerItemsContainer.removeChild(winnerItem); 0137 winnerItem = null; 0138 } 0139 } 0140 } 0141 0142 //============================================================ 0143 private function resetBtnClicked(e:MouseEvent):void { 0144 winnersBrowser.refreshWinnerItems(winnersNumMc.tf_num.text);//删除已有的winnerItems,并新建 0145 removeResultPanelChildren();//删除结果面板中奖结果 0146 0147 winnersBrowser.ary_guestIds = cloneObject(GobalData.ary_originalIds);//恢复抽奖类的嘉宾id数组为全部嘉宾id 0148 GobalData.ary_winnerIds = [];//清除所有抽出的中奖人id 0149 } 0150 0151 //============================================================ 0152 private function resultBtnClicked(e:MouseEvent):void { 0153 resultBtnCount++; 0154 if (resultBtnCount % 2 != 0){ 0155 TweenLite.to(resultPanel, 0.8, {alpha: 1, visible: true, x: 905, y: 650, scaleX: 1, scaleY: 1, ease: Strong.easeOut}); 0156 } else { 0157 TweenLite.to(resultPanel, 0.8, {alpha: 0, visible: true, x: 905, y: 680, scaleX: 0, scaleY: 0, ease: Strong.easeOut}); 0158 } 0159 } 0160 0161 private function addResultPanel():void { 0162 resultPanel = new ResultPanel(); 0163 addChild(resultPanel); 0164 resultPanel.x = 905; 0165 resultPanel.y = 680; 0166 resultPanel.scaleX = resultPanel.scaleY = 0; 0167 resultPanel.alpha = 0; 0168 resultPanel.visible = false; 0169 0170 resultWinnerItemsContainer = new Sprite(); 0171 resultPanel.addChild(resultWinnerItemsContainer); 0172 resultWinnerItemsContainer.x = -824; 0173 resultWinnerItemsContainer.y = -500; 0174 } 0175 0176 //============================================================ 0177 private function cloneObject(obj:Object):* { 0178 var copier:ByteArray = new ByteArray(); 0179 copier.writeObject(obj); 0180 copier.position = 0; 0181 return copier.readObject(); 0182 } 0183 0184 } 0185 }
抽奖类WinnersBrowser.as
0001 package lu.leon.display { 0002 import flash.display.Sprite; 0003 import flash.events.Event; 0004 import flash.events.TimerEvent; 0005 import flash.utils.Timer; 0006 import flash.utils.ByteArray; 0007 0008 import lu.leon.display.WinnerItem; 0009 0010 import com.greensock.TweenLite; 0011 import com.greensock.easing.*; 0012 0013 /** 0014 * @author LeonLu 0015 * @email [email protected] 0016 * @date 2010-12-15 10:16 0017 **/ 0018 //[SWF(width="",height="",frameRate="",backgroundColor="#")] 0019 public class WinnersBrowser extends Sprite { 0020 public var ary_guestIds:Array = [];//嘉宾id(动态更新) 0021 0022 public var itemsContainer:Sprite;//装winnerItem的容器 0023 0024 private var timer:Timer; 0025 0026 public function WinnersBrowser(){ 0027 if (stage) 0028 init(); 0029 else 0030 addEventListener(Event.ADDED_TO_STAGE, init); 0031 } 0032 0033 private function init(e:Event = null):void { 0034 removeEventListener(Event.ADDED_TO_STAGE, init); 0035 0036 ary_guestIds = cloneObject(GobalData.ary_originalIds); 0037 0038 itemsContainer = new Sprite(); 0039 addChild(itemsContainer); 0040 } 0041 0042 public function setData(sum:int):void { 0043 creatItems(sum); 0044 } 0045 0046 private function timerHandler(e:TimerEvent):void { 0047 getRandomNames(); 0048 } 0049 0050 public function startDrawing():void { 0051 timer = new Timer(50); 0052 timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true); 0053 timer.start(); 0054 } 0055 0056 public function stopDrawing():void { 0057 timer.stop(); 0058 0059 getRandomNames(true); 0060 } 0061 0062 private function getRandomNames(update:Boolean = false):void { 0063 for (var i:int = 0; i < itemsContainer.numChildren; i++){ 0064 var item:WinnerItem = itemsContainer.getChildAt(i) as WinnerItem; 0065 var randomIndex:int = Math.floor(Math.random() * ary_guestIds.length); 0066 var id:int = ary_guestIds[randomIndex]; 0067 var _name:String = GobalData.guestsXml.guest.(@id == id).@name; 0068 item.setData(_name); 0069 0070 if (update){ 0071 GobalData.ary_winnerIds.push(id); 0072 ary_guestIds.splice(randomIndex, 1); 0073 } 0074 } 0075 } 0076 0077 //============================================================ 0078 public function refreshWinnerItems(sum:int):void { 0079 removeItems(); 0080 creatItems(sum); 0081 } 0082 0083 //============================================================ 0084 private function creatItems(sum:int):void { 0085 var row:int = 0; 0086 var col:int = 0; 0087 0088 for (var i:int = 0; i < sum; i++){ 0089 var item:WinnerItem = new WinnerItem(); 0090 itemsContainer.addChild(item); 0091 0092 item.x = (item.width + 30) * col; 0093 item.y = (item.height + 20) * row; 0094 0095 col++; 0096 if (col == 5){ 0097 row++; 0098 col = 0; 0099 } 0100 } 0101 } 0102 0103 private function removeItems():void { 0104 var childNum:int = itemsContainer.numChildren; 0105 if (childNum > 0){ 0106 for (var i:int = 0; i < childNum; i++){ 0107 var item:WinnerItem = itemsContainer.getChildAt(0) as WinnerItem; 0108 itemsContainer.removeChild(item); 0109 item = null; 0110 } 0111 } 0112 } 0113 0114 //============================================================ 0115 private function cloneObject(obj:Object):* { 0116 var copier:ByteArray = new ByteArray(); 0117 copier.writeObject(obj); 0118 copier.position = 0; 0119 return copier.readObject(); 0120 } 0121 0122 } 0123 }