抽奖Flash - Main.as

 =====文件名:Main.as=====
package {
    import flash.display.Loader;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.utils.ByteArray;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.net.URLRequest;
    import flash.ui.Keyboard;
    import flash.system.fscommand;
    import lu.leon.StatisticsPanel;
    import lu.leon.WinnerItem;
    import lu.leon.LoadPicManager;
    import lu.leon.DataProxy;
    import lu.leon.WinnersBrowser;
    import com.greensock.TweenLite;
    import com.greensock.easing.*;
    /**
     * ...
     * @author LeonLu
     * @version 20101129
     */
    public class Main extends Sprite {
        private var prizeLevelMc:MovieClip;
        private var starsLoader:Loader;
        private var mainXmlPath:String = "xml/main.xml";
        private var guestsXmlPath:String = "xml/guests.xml";
        private var dataProxy_mainXml:DataProxy;
        private var dataProxy_guestsXml:DataProxy;
        private var sumOfGuests:int;
        private var winnersBrowser:WinnersBrowser;
        private var firstPrizeWinnersBrowser:WinnersBrowser;
        private var statisticsPanel:StatisticsPanel;
        private var tempBoo:Boolean = false;
        public function Main(){
            stage.focus = this;
            addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
        }
        private function addedToStage(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
            loadSwf();
            //luckyStepMc.alpha = 0;
            loadXml();
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true);
        }
        private function keyDownHandler(e:KeyboardEvent):void {
            switch (e.keyCode){
                case Keyboard.ESCAPE:
                    fscommand("closeFlashPlayer");
                    trace("closeFlashPlayer instruction is sending");
                    break;
                case Keyboard.DELETE:
                    /*                    GobalData.step = GobalData.sumOfPrizes - 1;
                       winnersBrowser.destroyThis();
                       TweenLite.to(this, 0, {delay: 1.5, onComplete: showIt});

                       prizeLevelMc.gotoAndStop(1);
                       wingsLoader.alpha = 1;
                     */
                    break;
            }
        }
        private function showIt():void {
            winnersBrowser.setData(GobalData.mainXml.winnersPanel.prize, GobalData.step);
        }
        private function loadXml():void {
            dataProxy_mainXml = new DataProxy(mainXmlPath);
            dataProxy_mainXml.addEventListener(DataProxy.XML_LOADED, mainXmlLoaded, false, 0, true);
            dataProxy_guestsXml = new DataProxy(guestsXmlPath);
            dataProxy_guestsXml.addEventListener(DataProxy.XML_LOADED, guestsXmlLoaded, false, 0, true);
        }
        private function mainXmlLoaded(e:Event):void {
            GobalData.mainXml = dataProxy_mainXml.xml;
            GobalData.sumOfPrizes = GobalData.mainXml.winnersPanel.prize.length();
            GobalData.step = GobalData.sumOfPrizes - 1;
            GobalData.prizeLevel = 4;
            addStatisticsPanel();
        }
        private function guestsXmlLoaded(e:Event):void {
            GobalData.guestsXml = dataProxy_guestsXml.xml;
            sumOfGuests = GobalData.guestsXml.guest.length();
            for (var i:int = 0; i < sumOfGuests; i++){
                var id:int = int(GobalData.guestsXml.guest[i].@id);
                GobalData.ary_orginalIds[i] = id;
                if (i == sumOfGuests - 1){
                    GobalData.ary_currentIds = cloneObject(GobalData.ary_orginalIds);
                    addWinnersBrowser();
                }
            }
        }
        /*        private function resetStatisticsPanel():void {
           statisticsPanel.y = -730;
           statisticsPanel.alpha = 0;
           }
         */
        private function addWinnersBrowser():void {
            winnersBrowser = new WinnersBrowser();
            winnersPanelContainer.addChild(winnersBrowser);
            winnersBrowser.setData(GobalData.mainXml.winnersPanel.prize, GobalData.step);
            GobalData.ary_winnerIds = winnersBrowser.ary_winnerIds;
            winnersBrowser.addEventListener(WinnersBrowser.SEND_INSTRUCTION, sendInstruction, false, 0, true);
            winnersBrowser.addEventListener(WinnersBrowser.DRAW_DONE, drawDone, false, 0, true);
            tf_numChildren.text = String(sumOfGuests);
        }
        //该函数执行的条件在WinnersBrowser类中已经定义,即当抽的是三等奖或幸运奖时,按空格,立刻发送指令
        private function sendInstruction(e:Event):void {
            var instruction:String;
            var sum:int;
            instruction = GobalData.mainXml.statisticsPanel.area[GobalData.prizeLevel].@title;
            sum = GobalData.mainXml.statisticsPanel.area[GobalData.prizeLevel].@sum;
            for (var i:int = 0; i < sum; i++){
                var winnerId:int = GobalData.ary_winnerIds[GobalData.prizeLevel][i];
                instruction += "|" + String(winnerId);
            }
            trace("sendInstruction:instruction=" + instruction);
            fscommand(instruction);
        }
        private function drawDone(e:Event):void {
            //trace("drawDone:GobalData.step=" + GobalData.step);
            //此处限制:抽幸运奖的情况此时不处理
            if (GobalData.step > 2){
                var instruction:String = GobalData.mainXml.winnersPanel.prize[GobalData.step].@title;
                for (var i:int = 0; i < GobalData.mainXml.winnersPanel.prize[GobalData.step].@sum; i++){
                    var winnerId:int = GobalData.ary_winnerIds[GobalData.step - 2][i];
                    instruction += "|" + String(winnerId);
                    (GobalData.ary_statistics[GobalData.step - 2][i] as WinnerItem).nameMc.tf_name.text = GobalData.guestsXml.guest.(@id == winnerId).@name;
                }
                if (GobalData.step != GobalData.sumOfPrizes - 1){
                    trace("drawDone:instruction=" + instruction);
                    fscommand(instruction);
                }
            }
            GobalData.step--;
            if (GobalData.step != 2 && GobalData.prizeLevel > 0){
                GobalData.prizeLevel--;
            }
            //trace("wddGobalData.step=" + GobalData.step);
            trace("2GobalData.step=" + GobalData.step);
            if (GobalData.step > -2 && GobalData.step != 2){
                TweenLite.killTweensOf(statisticsPanel);
                showStatisticsPanel(false);
                if (GobalData.step > 0){
                    //奖级标识MC
                    prizeLevelMc.gotoAndPlay((3 - GobalData.prizeLevel) * 8 + 1);
                    //此处设置,抽完特等奖并显示统计面板后,则显示幸运奖的【次数标识文本框】
                    if (GobalData.step == 1){
                        TweenLite.to(luckyStepMc, 1, {alpha: 1, delay: 0.5});
                    }
                } else {
                    //此处限制,抽第一批幸运奖时,是要重新建ary_winnerIds[GobalData.step] = [],之后2批不需要
                    GobalData.isLuckyPrize = true;
                    luckyStepMc.tf_num.text = String(2 - GobalData.step) + " / 3";
                }
                var index:int;
                if (GobalData.step > 2){
                    index = GobalData.step;
                } else {
                    //此时的else情况其实只包括GobalData.step<2,因为之前显示统计面板时让GobalData.step自减了一次,
                    //所以以后的抽奖winnersBrowser对应的xml索引号要+1,恢复正常
                    index = GobalData.step + 1;
                }
                winnersBrowser.setData(GobalData.mainXml.winnersPanel.prize, index);
            } else if (GobalData.step == 2){
                //抽完特等奖后,按←键,显示统计面板(特、一、二、三等奖统计结果)
                showStatisticsPanel(true);
            } else if (GobalData.step == -2){
                //抽完幸运奖后,按←键,显示统计面板(幸运奖统计结果)
                showStatisticsPanel(true, 2);
                TweenLite.to(statisticsPanel, 0.7, {alpha: 1, y: "+735", ease: Back.easeOut});
            }
        }
        private function showStatisticsPanel(isShowed:Boolean, bgPicIndex:int = 1):void {
            switch (isShowed){
                case true:
                    //显示统计面板中的幸运奖统计结果
                    if (bgPicIndex == 2){
                        statisticsPanel.showLuckyResult();
                        for (var i:int = 0; i < 60; i++){
                            var winnerId:int = GobalData.ary_winnerIds[0][i];
                            (GobalData.ary_statistics[0][i] as WinnerItem).nameMc.tf_name.text = GobalData.guestsXml.guest.(@id == winnerId).@name;
                        }
                    }
                    TweenLite.to(statisticsPanel, 0.7, {alpha: 1, y: "+735", ease: Back.easeOut});
                    break;
                case false:
                    statisticsPanel.alpha = 0;
                    statisticsPanel.x = 30;
                    statisticsPanel.y = -730;
                    break;
            }
        }
        //========================================================================
        private function loadSwf():void {
            starsLoader = new Loader();
            starsLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, starsLoaded, false, 0, true);
            starsLoader.load(new URLRequest("lib/swf/星星.swf"));
            var prizeLevelLoader:Loader = new Loader();
            prizeLevelLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, prizeLevelSwfLoaded, false, 0, true);
            prizeLevelLoader.load(new URLRequest("lib/swf/奖级.swf"));
        }
        private function starsLoaded(e:Event):void {
            addChild(starsLoader);
            starsLoader.x = -65;
            starsLoader.y = -100;
        }
        private function prizeLevelSwfLoaded(e:Event):void {
            prizeLevelMc = e.currentTarget.content as MovieClip;
            addChild(prizeLevelMc);
            addChild(luckyStepMc);
            prizeLevelMc.x = 390;
            prizeLevelMc.y = 203 - 60;
            prizeLevelMc.alpha = 0;
            TweenLite.to(prizeLevelMc, 0.6, {alpha: 1, y: "+60"});
            if (statisticsPanel){
                addChild(statisticsPanel);
                addChild(tf_test);
                addChild(tf_test2);
            }
        }
        private function addStatisticsPanel():void {
            statisticsPanel = new StatisticsPanel();
            addChild(statisticsPanel);
            showStatisticsPanel(false);
        }
        //========================================================================
        private function cloneObject(obj:Object):* {
            var copier:ByteArray = new ByteArray();
            copier.writeObject(obj);
            copier.position = 0;
            return copier.readObject();
        }
    }
}

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