一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

flex 實現(xiàn)植物大戰(zhàn)僵尸

 昵稱6916474 2011-06-21

package Tclass
{
        import flash.display.Sprite;

        import flash.utils.Timer;

        import flash.events.TimerEvent;

        import flash.events.MouseEvent;

        import flash.events.Event;

        import flash.text.TextField;

        public class Main extends Sprite {

                //一個2維數(shù)組用來存儲游戲區(qū)塊

                private var plantsArray:Array;// 種植在游戲區(qū)域里的植物

                private var zombiesArray:Array;//在游戲區(qū)域里的僵尸

                //

                // 計時器

                //

                private var flowersTimer:Timer=new Timer(5000);//計時器,使得陽光落下

                private var zombieTimer:Timer=new Timer(5000);//計時器,讓僵尸出場

                //

                // 容器

                //

                private var sunContainer:Sprite=new Sprite();// 所有陽光的容器

                private var plantContainer:Sprite=new Sprite();// 所有植物的容器

                public var bulletContainer:Sprite=new Sprite();// 所有子彈的容器

                private var zombieContainer:Sprite=new Sprite();// 所有僵尸的容器

                private var overlayContainer:Sprite=new Sprite();// 所有翻蓋物的容器

                //

                // 我們的演員

                //

                private var movingPlant:plantMc;// 玩家在游戲區(qū)域能夠拖動的植物

                private var selector:selectorMc;// 選擇器(一個高亮的區(qū)塊),告訴玩家他將把植物種在哪

                //

                // 其它變量

                //

                private var money:uint=0;// 玩家所擁有的金錢數(shù)量

                private var moneyText:TextField=new TextField  ;// 動態(tài)文本框,用來顯示玩家的金錢

                private var playerMoving:Boolean=false;// 布爾型變量,標志玩家是否在移動一個植物

                public function Main():void {

                        setupField();// 初始化游戲區(qū)塊

                        drawField();// 畫出游戲區(qū)塊

                        fallingSuns();// 初始化下落的陽光

                        addPlants();// 初始化植物

                        addZombies();// 初始化僵尸

                        addEventListener(Event.ENTER_FRAME,onEnterFrm);

                }

                //

                // 游戲區(qū)域設(shè)置,創(chuàng)建用來存儲植物和僵尸信息的數(shù)組

                //

                private function setupField():void {

                        plantsArray=new Array();

                        for (var i:uint=0; i<5; i++) {

                                plantsArray[i]=new Array();

                                for (var j:uint=0; j<9; j++) {

                                        plantsArray[i][j]=0;

                                }

                        }

                        zombiesArray=new Array(0,0,0,0,0);

                }

                //

                // 顯示玩家的金錢

                //

                private function updateMoney():void {

                        moneyText.text="Money: "+money.toString();

                }

                //

                // 畫出游戲區(qū)域

                //

                private function drawField():void {

                        var fieldSprite:Sprite=new Sprite();

                        var randomGreen:Number;

                        addChild(fieldSprite);

                        fieldSprite.graphics.lineStyle(1,0xFFFFFF);

                        for (var i:uint=0; i<5; i++) {

                                for (var j:uint=0; j<9; j++) {

                                        randomGreen=(125+Math.floor(Math.random()*50))*256;

                                        fieldSprite.graphics.beginFill(randomGreen);

                                        fieldSprite.graphics.drawRect(25+65*j,80+75*i,65,75);

                                }

                        }

                        addChild(sunContainer);

                        addChild(plantContainer);

                        addChild(bulletContainer);

                        addChild(zombieContainer);

                        addChild(overlayContainer);

                        overlayContainer.addChild(moneyText);

                        updateMoney();

                        moneyText.textColor=0xFFFFFF;

                        moneyText.height=20;

                }

                //

                // 初始化僵尸

                //

                private function addZombies():void {

                        zombieTimer.start();

                        zombieTimer.addEventListener(TimerEvent.TIMER,newZombie);

                }

                //

                // 增加一個新的僵尸

                //

                private function newZombie(e:TimerEvent):void {

                        var zombie:zombieMc=new zombieMc();// 構(gòu)造僵尸

                        zombieContainer.addChild(zombie);// 增加僵尸

                        zombie.zombieRow=Math.floor(Math.random()*5);// 生成隨機行數(shù),用于放置僵尸

                        zombiesArray[zombie.zombieRow]++;// 增加第row行的僵尸數(shù)量

                        zombie.x=660;// 把僵尸放在屏幕的右邊

                        zombie.y=zombie.zombieRow*75+115;

                }

                //

                // 初始化陽光

                //

                private function fallingSuns():void {

                        flowersTimer.start();

                        flowersTimer.addEventListener(TimerEvent.TIMER, newSun);

                }

                //

                // 增加一束新的陽光

                //

                private function newSun(e:TimerEvent):void {

                        var sunRow:uint=Math.floor(Math.random()*5);// 隨機行

                        var sunCol:uint=Math.floor(Math.random()*9);// 隨機列

                        var sun:sunMc = new sunMc();// 構(gòu)造陽光

                        sun.buttonMode=true;// 當鼠標滑過陽光時,改變鼠標的形狀

                        sunContainer.addChild(sun);// 增加陽光

                        sun.x=52+sunCol*65;// 把陽光放在合適的位置

                        sun.destinationY=130+sunRow*75;// 定義陽光destinationY屬性

                        sun.y=-20;// 把陽光放在舞臺頂部的上方

                        sun.addEventListener(MouseEvent.CLICK,sunClicked);// 給陽光注冊鼠標點擊事件

                }

                //

                // 陽光的鼠標點擊事件句柄

                //

                private function sunClicked(e:MouseEvent):void {

                        e.currentTarget.removeEventListener(MouseEvent.CLICK,sunClicked);// 移除鼠標事件偵聽

                        money+=5;//讓玩家賺到5個金幣

                        updateMoney();// 更新動態(tài)文本

                        var sunToRemove:sunMc=e.currentTarget as sunMc;// 獲得我們必須移除的陽光

                        sunContainer.removeChild(sunToRemove);// 移除該陽光

                }

                //

                // 創(chuàng)建一個植物欄,現(xiàn)在只有一種植物

                //

                private function addPlants():void {

                        var plant:plantMc=new plantMc();// 構(gòu)造一株新的植物

                        overlayContainer.addChild(plant);// 增加植物

                        plant.buttonMode=true;// 使鼠標改變形狀,當它滑過新植物時

                        plant.x=90;

                        plant.y=40;

                        plant.addEventListener(MouseEvent.CLICK,onPlantClicked);// 給新植物注冊鼠標點擊事件

                }

                //

                // 植物的鼠標點擊事件句柄

                //

                private function onPlantClicked(e:MouseEvent):void {

                        // 檢查玩家是否有足夠的錢(當前是10)來購買植物,并且是否正在拖動一個植物

                        if (money>=10&&! playerMoving) {

                                money-=10;// 付款

                                updateMoney();// 更新動態(tài)文本

                                selector=new selectorMc();// 創(chuàng)建一個新的選擇器

                                selector.visible=false;// 使選擇器不可見

                                overlayContainer.addChild(selector);// 把選擇器加入到顯示列表

                                movingPlant=new plantMc();// 構(gòu)建一個新的供玩家拖動的植物

                                movingPlant.addEventListener(MouseEvent.CLICK,placePlant);// 給該植物注冊一個鼠標點擊事件

                                overlayContainer.addChild(movingPlant);// 把該植物加入到顯示列表

                                playerMoving=true;// 告訴腳本正在移動一株植物

                        }

                }

                //

                // 把植物放置在游戲區(qū)域中

                //

                private function placePlant(e:MouseEvent):void {

                        var plantRow:int=Math.floor((mouseY-80)/75);

                        var plantCol:int=Math.floor((mouseX-25)/65);

                        // 檢查該區(qū)塊是否位于游戲區(qū)域內(nèi),并且該區(qū)塊沒有其它植物存在

                        if (plantRow>=0&&plantCol>=0&&plantRow<5&&plantCol<9&&plantsArray[plantRow][plantCol]==0) {

                                var placedPlant:plantMc=new plantMc();// 構(gòu)建一株植物,用來種植

                                placedPlant.fireRate=75;// 植物的開火速率,單位幀

                                placedPlant.recharge=0;// 當recharge 等于 fireRate時,植物已經(jīng)準備好開火了                       

                                                                placedPlant.isFiring=false;// 一個布爾變量來存儲植物是否正在開火

                                placedPlant.plantRow=plantRow;// 植物所在的行

                                plantContainer.addChild(placedPlant);// 把該植物加入到顯示列表

                                placedPlant.x=plantCol*65+57;

                                placedPlant.y=plantRow*75+115;

                                playerMoving=false;// 告訴腳本玩家不在移動植物了

                                movingPlant.removeEventListener(MouseEvent.CLICK,placePlant);// 移除事件偵聽

                                overlayContainer.removeChild(selector);// 移除選擇器

                                overlayContainer.removeChild(movingPlant);// 移除供拖動的植物

                                plantsArray[plantRow][plantCol]=1;// 更新游戲區(qū)塊信息

                        }

                }

                //

                // 游戲循環(huán),游戲的核心函數(shù)

                //

                private function onEnterFrm(e:Event):void {

                        var i:int;

                        var j:int;

                        //

                        // 植物管理

                        //

                        for (i=0; i<plantContainer.numChildren; i++) {

                                var currentPlant:plantMc=plantContainer.getChildAt(i) as plantMc;

                                // 讓我們看看植物是否能開火

                                if (zombiesArray[currentPlant.plantRow]>0&&currentPlant.recharge==currentPlant.fireRate&&! currentPlant.isFiring) {

                                        var bullet:bulletMc=new bulletMc();// 構(gòu)造一顆新的子彈

                                        bulletContainer.addChild(bullet);// 把子彈加入到顯示列表

                                        bullet.x=currentPlant.x;

                                        bullet.y=currentPlant.y;

                                        bullet.sonOf=currentPlant;// 存儲該子彈是由哪一株植物射出的

                                        currentPlant.recharge=0;// 重新準備開火

                                        currentPlant.isFiring=true;// 植物正在開火

                                }

                                if (currentPlant.recharge<currentPlant.fireRate) {

                                        currentPlant.recharge++;                                }

                        }

                        //

                        // 子彈管理

                        //

                        for (i=0; i<bulletContainer.numChildren; i++) {

                                var movingBullet:bulletMc=bulletContainer.getChildAt(i) as bulletMc;

                                movingBullet.x+=3;//把每個子彈向右移動3個像素

                                var firingPlant:plantMc=movingBullet.sonOf as plantMc;// 獲得這個子彈是哪個植物射擊的

                                // 讓我們看看子彈是否飛出了舞臺

                                if (movingBullet.x>650) {

                                        firingPlant.isFiring=false;// 植物不再處于正在開火的狀態(tài)

                                        bulletContainer.removeChild(movingBullet);// 移除子彈

                                } else {

                                        for (j=0; j<zombieContainer.numChildren; j++) {

                                                var movingZombie:zombieMc=zombieContainer.getChildAt(j) as zombieMc;

                                                // 讓我們看看植物是否被子彈擊中

                                                if (movingZombie.hitTestPoint(movingBullet.x,movingBullet.y,true)) {

                                                        movingZombie.alpha-=0.3;// 減少僵尸的能量(透明度)

                                                        firingPlant.isFiring=false;// 植物不再處于正在開火的狀態(tài)

                                                        bulletContainer.removeChild(movingBullet);// 移除子彈       

                                                               // 讓我們看看僵尸的能量(透明度)是否降至為0了

                                                        if (movingZombie.alpha<0) {

                                                                zombiesArray[movingZombie.zombieRow]--;// 減少該行僵尸的數(shù)量

                                                                zombieContainer.removeChild(movingZombie);// 移除僵尸                                                        }

                                                        break;

                                                }

                                        }

                                }

                        }

                        //

                        // 僵尸管理

                              //

                        for (i=0; i<zombieContainer.numChildren; i++) {

                                movingZombie=zombieContainer.getChildAt(i) as zombieMc;

                                movingZombie.x-=0.5;// 每一個僵尸往左移動0.5個像素

                        }

                        //

                        // 陽光管理

                        //

                        for (i=0; i<sunContainer.numChildren; i++) {

                                var fallingSun:sunMc=sunContainer.getChildAt(i) as sunMc;

                                // 讓我們看看陽光是否還在下落

                                if (fallingSun.y<fallingSun.destinationY) {

                                        fallingSun.y++;// 把陽光往下移動一個像素

                                } else {

                                        fallingSun.alpha-=0.01;// 使陽光淡出

                                        // 檢查陽光是否消失了

                                        if (fallingSun.alpha<0) {

                                                fallingSun.removeEventListener(MouseEvent.CLICK,sunClicked);// 移除事件偵聽

                                                sunContainer.removeChild(fallingSun);// 移出顯示列表

                                        }

                                }

                        }

                        //

                        // 安置植物

                        //

                        if (playerMoving) {

                                movingPlant.x=mouseX;

                                movingPlant.y=mouseY;

                                var plantRow:int=Math.floor((mouseY-80)/75);

                                var plantCol:int=Math.floor((mouseX-25)/65);

                                // 檢查是否在游戲區(qū)域內(nèi)

                                if (plantRow>=0&&plantCol>=0&&plantRow<5&&plantCol<9) {

                                        selector.visible=true;// 顯示選擇器

                                        selector.x=25+plantCol*65;

                                        selector.y=80+plantRow*75;

                                } else {

                                        selector.visible=false;//隱藏選擇器

                                }

                        }

                }

        }

 

}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    午夜福利92在线观看| 麻豆看片麻豆免费视频| 粉嫩国产一区二区三区在线| 99久久无色码中文字幕免费| 在线观看国产成人av天堂野外| 色综合久久六月婷婷中文字幕| 国产大屁股喷水在线观看视频| 日本精品理论在线观看| 暴力三级a特黄在线观看| 中文字幕一区二区三区中文| 一区二区三区免费公开| 亚洲国产成人av毛片国产| 成人三级视频在线观看不卡| 国产在线不卡中文字幕| 超薄丝袜足一区二区三区| 91精品国自产拍老熟女露脸 | 美女黄色三级深夜福利| 国产亚洲二区精品美女久久 | 欧美国产日本高清在线| 亚洲欧美日韩综合在线成成| 亚洲中文字幕在线视频频道| 超薄肉色丝袜脚一区二区| 国内自拍偷拍福利视频| 亚洲精品熟女国产多毛| 国产不卡视频一区在线| 欧美日韩中黄片免费看| 狠狠亚洲丁香综合久久| 有坂深雪中文字幕亚洲中文 | 成人午夜免费观看视频| 丝袜av一区二区三区四区五区| 丰满少妇高潮一区二区| 超碰在线播放国产精品| 国产精品人妻熟女毛片av久| 久久热麻豆国产精品视频| 夜色福利久久精品福利| 98精品永久免费视频| 精品久久久一区二区三| 麻豆一区二区三区在线免费| 亚洲天堂精品一区二区| 午夜福利黄片免费观看| 亚洲精品小视频在线观看|