抽奖Flash - WinnersBrowser.as

 =====文件名:WinnersBrowser.as=====
package lu.leon {
    import com.greensock.events.LoaderEvent;
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.TimerEvent;
    import flash.ui.Keyboard;
    import flash.utils.Timer;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import com.greensock.TweenLite;
    import com.greensock.easing.*;
    /**
     * ...
     * @author LeonLu
     * @version 20101129
     */
    public class WinnersBrowser extends Sprite {
        public static const DRAW_DONE:String = "draw_done";
        public static const SEND_INSTRUCTION:String = "send_instruction";
        public static const SHOW_STATISTICS:String = "show_statistics";
        public var ary_winnerIds:Array; //中奖人id
        public var ary_winners:Array; //中奖人
        private var xml:XML;
        private var level:int; //当前所处的奖级        
        private var sumOfItems:int;
        private var itemsContainer:Sprite;
        private var maxW:int;
        private var maxH:int;
        private var cols:int;
        private var startX:int;
        private var startY:int;
        private var hSpace:int;
        private var vSpace:int;
        private var timer:Timer;
        private var redrawTimer:Timer;
        private var currentWinnerIndex:int; //中奖人不在场,需要重新抽取当前位置的新的中奖人
        //从数字键1(对应49)起步算的中奖人总数对应的数字键的keyCode,例如:如果oldNums=2,maxNumberKeyCode=49+1
        private var maxNumberKeyCode:uint;
        private var currentStatus:String;
        private var enterEnabled:Boolean = true;
        private var numberEnabled:Boolean = false;
        private var spaceEnabled:Boolean = false;
        private var leftEnabled:Boolean = false;
        private var isLooping:Boolean = false;
        private var tweenCounter:int;
        private var snd_start:MySoundPlayer;
        private var snd_stop:MySoundPlayer;
        private var snd_showStatistics:MySoundPlayer;
        public function WinnersBrowser(){
            level = GobalData.step;
            ary_winnerIds = new Array(5);
            ary_winners = new Array(5);
            itemsContainer = new Sprite();
            addChild(itemsContainer);
            addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
        }
        private function addedToStage(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
            loadSounds(); //载入声音,但并不播放
        }
        public function setData(xmlList:XMLList, index:int):void {
            //trace("itemsContainer.numChildren=" + itemsContainer.numChildren);
            xml = xmlList[index];
            if (!GobalData.isLuckyPrize){
                ary_winnerIds[GobalData.prizeLevel] = [];
                ary_winners[GobalData.prizeLevel] = [];
            }
            maxNumberKeyCode = 49 + (xml.@sum - 1);
            cols = xml.@cols;
            hSpace = xml.@horizontalSpace;
            vSpace = xml.@verticalSpace;
            //判断是不是第一次抽奖,如果是,则不需要等待1秒钟载入winnterItem,
            //否则,先删除之前存在的winnterItems后,等待1秒钟后再载入新的winnterItems
            var delayTime:Number;
            if (GobalData.step < GobalData.sumOfPrizes - 1){
                removeItems();
                delayTime = 1;
            } else if (GobalData.step == GobalData.sumOfPrizes - 1){
                delayTime = 0;
            }
            tweenCounter = 0; //每次执行setData()方法,都将tweenCounter=0
            sumOfItems = xml.@sum;
            //trace("sumOfItems=" + sumOfItems);
            TweenLite.to(this, 0, {delay: delayTime, onComplete: showIt});
            /*var subtraction:int = sum - sumOfItems;
               if (subtraction > 0){
               while (itemsContainer.numChildren) {
               trace("delete item");
               itemsContainer.removeChildAt(0);
               }
               for (var i:int = 0; i < sum; i++) {
               trace("add item");
               var item:WinnerItem = new WinnerItem();
               item.id = i + 1;
               itemsContainer.addChild(item);
               }

               } else if(subtraction < 0){
               deleteRedundantWinnerItems(sumOfItems - 1, sum);
               }
             */
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true);
        }
        private function showIt():void {
            //trace("sumOfItems="+sumOfItems);
            for (var i:int = 0; i < sumOfItems; i++){
                var item:WinnerItem;
                if (GobalData.step == GobalData.sumOfPrizes - 1){
                    item = new WinnerItem("big");
                } else if (GobalData.step > 2){
                    item = new WinnerItem("biggest");
                } else if (GobalData.step <= 2){
                    item = new WinnerItem("middle");
                }
                if (GobalData.step != 6 && GobalData.step > 2){
                    item.id = i + 1;
                }
                item.alpha = 0;
                itemsContainer.addChild(item);
            }
            //trace("itemsContainer.numChildren=" + itemsContainer.numChildren);
            if (GobalData.step == GobalData.sumOfPrizes - 1){
                maxW = 1065;
                maxH = 390;
            }
            if (GobalData.step > 2){
                maxW = 1025;
                maxH = 390;
            } else {
                maxW = 1045;
                maxH = 430;
            }
            ArrangePosition.ExecuteIt(itemsContainer, cols, hSpace, vSpace, maxW, maxH, "items");
        }
        /*private function deleteRedundantWinnerItems(totalNums:int, subtraction:int):void {
           for (var i:int = totalNums; i > subtraction; i--){
           itemsContainer.removeChild(itemsContainer.getChildAt(i));
           }
           }
         */
        private function keyDownHandler(e:KeyboardEvent):void {
            if (e.keyCode == Keyboard.ENTER && enterEnabled){
                currentStatus = "enter";
                startDrawing();
                isLooping = true;
                enterEnabled = false;
                spaceEnabled = true;
            } else if (e.keyCode >= 49 && e.keyCode <= maxNumberKeyCode && numberEnabled && !isLooping){
                currentStatus = "number";
                isLooping = true;
                spaceEnabled = true;
                currentWinnerIndex = int(String.fromCharCode(e.keyCode)) - 1;
                startDrawingAlone(currentWinnerIndex);
            } else if (e.keyCode == Keyboard.SPACE && spaceEnabled){
                spaceEnabled = false;
                leftEnabled = true;
                numberEnabled = true;
                isLooping = false;
                switch (currentStatus){
                    case "enter":
                        stopDrawing();
                        if (GobalData.step == GobalData.sumOfPrizes - 1 || GobalData.step == -1){
                            dispatchEvent(new Event(SEND_INSTRUCTION));
                        }
                        break;
                    case "number":
                        stopDrawingAlone();
                        break;
                }
            } else if (e.keyCode == Keyboard.LEFT && !isLooping && leftEnabled){
                //此处是先用之前的GobalData.step决定一些按键的可操作性,
                //然后再发送事件以改变GobalData.step和创建新的抽奖winnerItems
                numberEnabled = false;
                trace("1leftEnabled=" + leftEnabled);
                trace("1GobalData.step=" + GobalData.step);
                if (GobalData.step != 3){
                    leftEnabled = false;
                    if (GobalData.step > -1){
                        enterEnabled = true; //这个标识打开后,则又触发ENTER按键,相当于触发下个级别抽奖了
                    }
                }
                trace("2leftEnabled=" + leftEnabled);
                if (GobalData.step == 3 || GobalData.step == -1){
                    disposeSounds("showStatistics");
                }
                dispatchEvent(new Event(DRAW_DONE));
            }
        }
        //========================================================================
        //嘉宾人名滚动,在此处未设置不重复人名
        private function loopNames(e:TimerEvent):void {
            for (var i:int = 0; i < sumOfItems; i++){
                var randomId:int = getRandomId();
                var guest:String = GobalData.guestsXml.guest.(@id == randomId).@name;
                if (guest == ""){
                    guest = "********";
                        //trace("loopNames:guest=" + guest);
                }
                (itemsContainer.getChildAt(i) as WinnerItem).setData(guest);
            }
        }
        private function startDrawing():void {
            timer = new Timer(30);
            timer.addEventListener(TimerEvent.TIMER, loopNames, false, 0, true);
            timer.start();
            disposeSounds("start");
        }
        private function stopDrawing():void {
            timer.stop();
            timer.removeEventListener(TimerEvent.TIMER, loopNames);
            disposeSounds("stop");
            for (var i:int = 0; i < sumOfItems; i++){
                var randomId:int = getRandomId(true);
                //trace("randomId="+randomId);                
                var guest:String = GobalData.guestsXml.guest.(@id == randomId).@name;
                //trace("guest="+guest);
                (itemsContainer.getChildAt(i) as WinnerItem).setData(guest);
                ary_winnerIds[GobalData.prizeLevel].push(randomId);
                ary_winners[GobalData.prizeLevel].push(guest);
            }
        }
        //========================================================================
        private function loopNamesAlone(e:TimerEvent):void {
            var randomId:int = getRandomId();
            var guest:String = GobalData.guestsXml.guest.(@id == randomId).@name;
            (itemsContainer.getChildAt(currentWinnerIndex) as WinnerItem).setData(guest);
        }
        private function startDrawingAlone(index:int):void {
            redrawTimer = new Timer(30);
            redrawTimer.addEventListener(TimerEvent.TIMER, loopNamesAlone, false, 0, true);
            redrawTimer.start();
            disposeSounds("start");
        }
        private function stopDrawingAlone():void {
            redrawTimer.stop();
            redrawTimer.removeEventListener(TimerEvent.TIMER, loopNamesAlone);
            disposeSounds("stop");
            var randomId:int = getRandomId(true);
            var guest:String = GobalData.guestsXml.guest.(@id == randomId).@name;
            (itemsContainer.getChildAt(currentWinnerIndex) as WinnerItem).setData(guest);
            if (GobalData.step > 2){
                ary_winnerIds[GobalData.prizeLevel][currentWinnerIndex] = randomId;
                ary_winners[GobalData.prizeLevel][currentWinnerIndex] = guest;
            } else {
                //var luckyIndex:int = (3 - GobalData.step) * 20 + currentWinnerIndex;
                //ary_winnerIds[0][currentWinnerIndex] = randomId;
                //ary_winners[0][currentWinnerIndex] = guest;
            }
        }
        private function getRandomId(update:Boolean = false):int {
            var currentId:int;
            var randomIndex:int = Math.floor(Math.random() * GobalData.ary_currentIds.length);
            currentId = GobalData.ary_currentIds[randomIndex];
            if (update){
                GobalData.ary_currentIds.splice(randomIndex, 1);
            }
            return currentId;
        }
        //========================================================================
        private function removeItems(hasEffect:Boolean = true):void {
            /*            for (var i:int = sumOfItems - 1; i >= 0; i--){
               var item:WinnerItem = itemsContainer.getChildAt(i) as WinnerItem;
               TweenLite.to(item, 0, {alpha: 0, y: "+80", ease: Back.easeIn, onComplete: destroyWinnerItems});
               }
             */
            var sumOfChild:int = itemsContainer.numChildren;
            for (var j:int = 0; j < sumOfChild; j++){
                var item:WinnerItem = itemsContainer.getChildAt(0) as WinnerItem;
                itemsContainer.removeChild(item);
                item = null;
            }
        }
        private function destroyWinnerItems():void {
            tweenCounter++;
            if (tweenCounter == sumOfItems){
                var sumOfChild:int = itemsContainer.numChildren;
                //trace("删除之前:sumOfChild=" + sumOfChild);
                if (sumOfChild > 0){
                    for (var i:int = 0; i < sumOfChild; i++){
                        var item:WinnerItem = itemsContainer.getChildAt(0) as WinnerItem;
                        itemsContainer.removeChild(item);
                        item = null;
                    }
                }
            }
        }
        //========================================================================
        private function loadSounds():void {
            snd_start = new MySoundPlayer();
            snd_start.setData("lib/sounds/开始抽奖.mp3");
            snd_stop = new MySoundPlayer();
            snd_stop.setData("lib/sounds/停止抽奖.mp3");
            snd_showStatistics = new MySoundPlayer();
            snd_showStatistics.setData("lib/sounds/显示统计面板.mp3");
        }
        private function disposeSounds(instruction:String):void {
            switch (instruction){
                case "start":
                    snd_start.playIt(0, 100);
                    break;
                case "stop":
                    snd_start.stopIt();
                    snd_stop.playIt();
                    break;
                case "showStatistics":
                    snd_showStatistics.playIt();
                    break;
                case "closeAll":
                    snd_start.stopIt();
                    snd_stop.stopIt();
                    snd_showStatistics.stopIt();
                    break;
            }
        }
        //========================================================================
        public function destroyThis():void {
            if (timer){
                timer.stop();
            }
            if (redrawTimer){
                redrawTimer.stop();
            }
            disposeSounds("closeAll");
            isLooping = false;
            enterEnabled = true;
            spaceEnabled = false;
            leftEnabled = false;
            removeItems(false);
            tweenCounter = 0;
            ary_winnerIds = [];
            ary_winners = [];
        }
    }
}

你可能感兴趣的:(职场,休闲,抽奖Flash)