乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      flex 實(shí)現(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;// 布爾型變量,標(biāo)志玩家是否在移動一個植物

                      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);// 生成隨機(jī)行數(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);// 隨機(jī)行

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

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

                              sun.buttonMode=true;// 當(dāng)鼠標(biāo)滑過陽光時,改變鼠標(biāo)的形狀

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

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

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

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

                              sun.addEventListener(MouseEvent.CLICK,sunClicked);// 給陽光注冊鼠標(biāo)點(diǎn)擊事件

                      }

                      //

                      // 陽光的鼠標(biāo)點(diǎn)擊事件句柄

                      //

                      private function sunClicked(e:MouseEvent):void {

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

                              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;// 使鼠標(biāo)改變形狀,當(dāng)它滑過新植物時

                              plant.x=90;

                              plant.y=40;

                              plant.addEventListener(MouseEvent.CLICK,onPlantClicked);// 給新植物注冊鼠標(biāo)點(diǎn)擊事件

                      }

                      //

                      // 植物的鼠標(biāo)點(diǎn)擊事件句柄

                      //

                      private function onPlantClicked(e:MouseEvent):void {

                              // 檢查玩家是否有足夠的錢(當(dāng)前是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);// 給該植物注冊一個鼠標(biāo)點(diǎn)擊事件

                                      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;// 當(dāng)recharge 等于 fireRate時,植物已經(jīng)準(zhǔn)備好開火了                       

                                                                      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;// 重新準(zhǔn)備開火

                                              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ā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多