做一个像植物大战僵尸的Flash游戏4

本文详细介绍了如何使用ActionScript编程让植物在游戏中攻击僵尸。包括植物开火的条件、子弹的行为逻辑、僵尸的移动及被消灭的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 http://bbs.9ria.com/thread-82609-1-4.html

欢迎来到第四步。在这个步骤里,我们会使得植物能够开火,并且最终杀死僵尸。
让我们先来明确一下什么时候植能开火:
*当至少有一个僵尸与植物处于同一行
*植物一次只能射出一颗子弹
*距离上一次射击必须过去一定的时间

现在让我们来定义一下子弹的活动:
*子弹从左往右飞
*当子弹击中一个僵尸时被移除
*当子弹飞到舞台之外时被移除

这6个概念给我们的脚本带来了一些大的改动。意识到把所有的代码写到一个类里使得脚本变得很混乱,我尽力以最清晰的方式来组织它。我尽我最大所能来使得它保持可读性。

我创建了一个叫做bulletMc的对象,它代表了子弹。

准备好去看一个接近300行的代码吗?

  1. package {
  2.         import flash.display.Sprite;
  3.         import flash.utils.Timer;
  4.         import flash.events.TimerEvent;
  5.         import flash.events.MouseEvent;
  6.         import flash.events.Event;
  7.         import flash.text.TextField;
  8.         public class Main extends Sprite {
  9.                 //一个2维数组用来存储游戏区块
  10.                 private var plantsArray:Array;// 种植在游戏区域里的植物
  11.                 private var zombiesArray:Array;//在游戏区域里的僵尸
  12.                 //
  13.                 // 计时器
  14.                 //
  15.                 private var flowersTimer:Timer=new Timer(5000);//计时器,使得阳光落下
  16.                 private var zombieTimer:Timer=new Timer(5000);//计时器,让僵尸出场
  17.                 //
  18.                 // 容器
  19.                 //
  20.                 private var sunContainer:Sprite=new Sprite();// 所有阳光的容器
  21.                 private var plantContainer:Sprite=new Sprite();// 所有植物的容器
  22.                 public var bulletContainer:Sprite=new Sprite();// 所有子弹的容器
  23.                 private var zombieContainer:Sprite=new Sprite();// 所有僵尸的容器
  24.                 private var overlayContainer:Sprite=new Sprite();// 所有翻盖物的容器
  25.                 //
  26.                 // 我们的演员
  27.                 //
  28.                 private var movingPlant:plantMc;// 玩家在游戏区域能够拖动的植物
  29.                 private var selector:selectorMc;// 选择器(一个高亮的区块),告诉玩家他将把植物种在哪
  30.                 //
  31.                 // 其它变量
  32.                 //
  33.                 private var money:uint=0;// 玩家所拥有的金钱数量
  34.                 private var moneyText:TextField=new TextField  ;// 动态文本框,用来显示玩家的金钱
  35.                 private var playerMoving:Boolean=false;// 布尔型变量,标志玩家是否在移动一个植物
  36.                 public function Main():void {
  37.                         setupField();// 初始化游戏区块
  38.                         drawField();// 画出游戏区块
  39.                         fallingSuns();// 初始化下落的阳光
  40.                         addPlants();// 初始化植物
  41.                         addZombies();// 初始化僵尸
  42.                         addEventListener(Event.ENTER_FRAME,onEnterFrm);
  43.                 }
  44.                 //
  45.                 // 游戏区域设置,创建用来存储植物和僵尸信息的数组
  46.                 //
  47.                 private function setupField():void {
  48.                         plantsArray=new Array();
  49.                         for (var i:uint=0; i<5; i++) {
  50.                                 plantsArray[i]=new Array();
  51.                                 for (var j:uint=0; j<9; j++) {
  52.                                         plantsArray[i][j]=0;
  53.                                 }
  54.                         }
  55.                         zombiesArray=new Array(0,0,0,0,0);
  56.                 }
  57.                 //
  58.                 // 显示玩家的金钱
  59.                 //
  60.                 private function updateMoney():void {
  61.                         moneyText.text="Money: "+money.toString();
  62.                 }
  63.                 //
  64.                 // 画出游戏区域
  65.                 //
  66.                 private function drawField():void {
  67.                         var fieldSprite:Sprite=new Sprite();
  68.                         var randomGreen:Number;
  69.                         addChild(fieldSprite);
  70.                         fieldSprite.graphics.lineStyle(1,0xFFFFFF);
  71.                         for (var i:uint=0; i<5; i++) {
  72.                                 for (var j:uint=0; j<9; j++) {
  73.                                         randomGreen=(125+Math.floor(Math.random()*50))*256;
  74.                                         fieldSprite.graphics.beginFill(randomGreen);
  75.                                         fieldSprite.graphics.drawRect(25+65*j,80+75*i,65,75);
  76.                                 }
  77.                         }
  78.                         addChild(sunContainer);
  79.                         addChild(plantContainer);
  80.                         addChild(bulletContainer);
  81.                         addChild(zombieContainer);
  82.                         addChild(overlayContainer);
  83.                         overlayContainer.addChild(moneyText);
  84.                         updateMoney();
  85.                         moneyText.textColor=0xFFFFFF;
  86.                         moneyText.height=20;
  87.                 }
  88.                 //
  89.                 // 初始化僵尸
  90.                 //
  91.                 private function addZombies():void {
  92.                         zombieTimer.start();
  93.                         zombieTimer.addEventListener(TimerEvent.TIMER,newZombie);
  94.                 }
  95.                 //
  96.                 // 增加一个新的僵尸
  97.                 //
  98.                 private function newZombie(e:TimerEvent):void {
  99.                         var zombie:zombieMc=new zombieMc();// 构造僵尸
  100.                         zombieContainer.addChild(zombie);// 增加僵尸
  101.                         zombie.zombieRow=Math.floor(Math.random()*5);// 生成随机行数,用于放置僵尸
  102.                         zombiesArray[zombie.zombieRow]++;// 增加第row行的僵尸数量
  103.                         zombie.x=660;// 把僵尸放在屏幕的右边
  104.                         zombie.y=zombie.zombieRow*75+115;
  105.                 }
  106.                 //
  107.                 // 初始化阳光
  108.                 //
  109.                 private function fallingSuns():void {
  110.                         flowersTimer.start();
  111.                         flowersTimer.addEventListener(TimerEvent.TIMER, newSun);
  112.                 }
  113.                 //
  114.                 // 增加一束新的阳光
  115.                 //
  116.                 private function newSun(e:TimerEvent):void {
  117.                         var sunRow:uint=Math.floor(Math.random()*5);// 随机行
  118.                         var sunCol:uint=Math.floor(Math.random()*9);// 随机列
  119.                         var sun:sunMc = new sunMc();// 构造阳光
  120.                         sun.buttonMode=true;// 当鼠标滑过阳光时,改变鼠标的形状
  121.                         sunContainer.addChild(sun);// 增加阳光
  122.                         sun.x=52+sunCol*65;// 把阳光放在合适的位置
  123.                         sun.destinationY=130+sunRow*75;// 定义阳光destinationY属性
  124.                         sun.y=-20;// 把阳光放在舞台顶部的上方
  125.                         sun.addEventListener(MouseEvent.CLICK,sunClicked);// 给阳光注册鼠标点击事件
  126.                 }
  127.                 //
  128.                 // 阳光的鼠标点击事件句柄
  129.                 //
  130.                 private function sunClicked(e:MouseEvent):void {
  131.                         e.currentTarget.removeEventListener(MouseEvent.CLICK,sunClicked);// 移除鼠标事件侦听
  132.                         money+=5;//让玩家赚到5个金币
  133.                         updateMoney();// 更新动态文本
  134.                         var sunToRemove:sunMc=e.currentTarget as sunMc;// 获得我们必须移除的阳光
  135.                         sunContainer.removeChild(sunToRemove);// 移除该阳光
  136.                 }
  137.                 //
  138.                 // 创建一个植物栏,现在只有一种植物
  139.                 //
  140.                 private function addPlants():void {
  141.                         var plant:plantMc=new plantMc();// 构造一株新的植物
  142.                         overlayContainer.addChild(plant);// 增加植物
  143.                         plant.buttonMode=true;// 使鼠标改变形状,当它滑过新植物时
  144.                         plant.x=90;
  145.                         plant.y=40;
  146.                         plant.addEventListener(MouseEvent.CLICK,onPlantClicked);// 给新植物注册鼠标点击事件
  147.                 }
  148.                 //
  149.                 // 植物的鼠标点击事件句柄
  150.                 //
  151.                 private function onPlantClicked(e:MouseEvent):void {
  152.                         // 检查玩家是否有足够的钱(当前是10)来购买植物,并且是否正在拖动一个植物
  153.                         if (money>=10&&! playerMoving) {
  154.                                 money-=10;// 付款
  155.                                 updateMoney();// 更新动态文本
  156.                                 selector=new selectorMc();// 创建一个新的选择器
  157.                                 selector.visible=false;// 使选择器不可见
  158.                                 overlayContainer.addChild(selector);// 把选择器加入到显示列表
  159.                                 movingPlant=new plantMc();// 构建一个新的供玩家拖动的植物
  160.                                 movingPlant.addEventListener(MouseEvent.CLICK,placePlant);// 给该植物注册一个鼠标点击事件
  161.                                 overlayContainer.addChild(movingPlant);// 把该植物加入到显示列表
  162.                                 playerMoving=true;// 告诉脚本正在移动一株植物
  163.                         }
  164.                 }
  165.                 //
  166.                 // 把植物放置在游戏区域中
  167.                 //
  168.                 private function placePlant(e:MouseEvent):void {
  169.                         var plantRow:int=Math.floor((mouseY-80)/75);
  170.                         var plantCol:int=Math.floor((mouseX-25)/65);
  171.                         // 检查该区块是否位于游戏区域内,并且该区块没有其它植物存在
  172.                         if (plantRow>=0&&plantCol>=0&&plantRow<5&&plantCol<9&&plantsArray[plantRow][plantCol]==0) {
  173.                                 var placedPlant:plantMc=new plantMc();// 构建一株植物,用来种植
  174.                                 placedPlant.fireRate=75;// 植物的开火速率,单位帧
  175.                                 placedPlant.recharge=0;// 当recharge 等于 fireRate时,植物已经准备好开火了                       
  176.                                                                 placedPlant.isFiring=false;// 一个布尔变量来存储植物是否正在开火
  177.                                 placedPlant.plantRow=plantRow;// 植物所在的行
  178.                                 plantContainer.addChild(placedPlant);// 把该植物加入到显示列表
  179.                                 placedPlant.x=plantCol*65+57;
  180.                                 placedPlant.y=plantRow*75+115;
  181.                                 playerMoving=false;// 告诉脚本玩家不在移动植物了
  182.                                 movingPlant.removeEventListener(MouseEvent.CLICK,placePlant);// 移除事件侦听
  183.                                 overlayContainer.removeChild(selector);// 移除选择器
  184.                                 overlayContainer.removeChild(movingPlant);// 移除供拖动的植物
  185.                                 plantsArray[plantRow][plantCol]=1;// 更新游戏区块信息
  186.                         }
  187.                 }
  188.                 //
  189.                 // 游戏循环,游戏的核心函数
  190.                 //
  191.                 private function onEnterFrm(e:Event):void {
  192.                         var i:int;
  193.                         var j:int;
  194.                         //
  195.                         // 植物管理
  196.                         //
  197.                         for (i=0; i<plantContainer.numChildren; i++) {
  198.                                 var currentPlant:plantMc=plantContainer.getChildAt(i) as plantMc;
  199.                                 // 让我们看看植物是否能开火
  200.                                 if (zombiesArray[currentPlant.plantRow]>0&&currentPlant.recharge==currentPlant.fireRate&&! currentPlant.isFiring) {
  201.                                         var bullet:bulletMc=new bulletMc();// 构造一颗新的子弹
  202.                                         bulletContainer.addChild(bullet);// 把子弹加入到显示列表
  203.                                         bullet.x=currentPlant.x;
  204.                                         bullet.y=currentPlant.y;
  205.                                         bullet.sonOf=currentPlant;// 存储该子弹是由哪一株植物射出的
  206.                                         currentPlant.recharge=0;// 重新准备开火
  207.                                         currentPlant.isFiring=true;// 植物正在开火
  208.                                 }
  209.                                 if (currentPlant.recharge<currentPlant.fireRate) {
  210.                                         currentPlant.recharge++;                                }
  211.                         }
  212.                         //
  213.                         // 子弹管理
  214.                         //
  215.                         for (i=0; i<bulletContainer.numChildren; i++) {
  216.                                 var movingBullet:bulletMc=bulletContainer.getChildAt(i) as bulletMc;
  217.                                 movingBullet.x+=3;//把每个子弹向右移动3个像素
  218.                                 var firingPlant:plantMc=movingBullet.sonOf as plantMc;// 获得这个子弹是哪个植物射击的
  219.                                 // 让我们看看子弹是否飞出了舞台
  220.                                 if (movingBullet.x>650) {
  221.                                         firingPlant.isFiring=false;// 植物不再处于正在开火的状态
  222.                                         bulletContainer.removeChild(movingBullet);// 移除子弹
  223.                                 } else {
  224.                                         for (j=0; j<zombieContainer.numChildren; j++) {
  225.                                                 var movingZombie:zombieMc=zombieContainer.getChildAt(j) as zombieMc;
  226.                                                 // 让我们看看植物是否被子弹击中
  227.                                                 if (movingZombie.hitTestPoint(movingBullet.x,movingBullet.y,true)) {
  228.                                                         movingZombie.alpha-=0.3;// 减少僵尸的能量(透明度)
  229.                                                         firingPlant.isFiring=false;// 植物不再处于正在开火的状态
  230.                                                         bulletContainer.removeChild(movingBullet);// 移除子弹       
  231.                                                                // 让我们看看僵尸的能量(透明度)是否降至为0了
  232.                                                         if (movingZombie.alpha<0) {
  233.                                                                 zombiesArray[movingZombie.zombieRow]--;// 减少该行僵尸的数量
  234.                                                                 zombieContainer.removeChild(movingZombie);// 移除僵尸                                                        }
  235.                                                         break;
  236.                                                 }
  237.                                         }
  238.                                 }
  239.                         }
  240.                         //
  241.                         // 僵尸管理
  242.                               //
  243.                         for (i=0; i<zombieContainer.numChildren; i++) {
  244.                                 movingZombie=zombieContainer.getChildAt(i) as zombieMc;
  245.                                 movingZombie.x-=0.5;// 每一个僵尸往左移动0.5个像素
  246.                         }
  247.                         //
  248.                         // 阳光管理
  249.                         //
  250.                         for (i=0; i<sunContainer.numChildren; i++) {
  251.                                 var fallingSun:sunMc=sunContainer.getChildAt(i) as sunMc;
  252.                                 // 让我们看看阳光是否还在下落
  253.                                 if (fallingSun.y<fallingSun.destinationY) {
  254.                                         fallingSun.y++;// 把阳光往下移动一个像素
  255.                                 } else {
  256.                                         fallingSun.alpha-=0.01;// 使阳光淡出
  257.                                         // 检查阳光是否消失了
  258.                                         if (fallingSun.alpha<0) {
  259.                                                 fallingSun.removeEventListener(MouseEvent.CLICK,sunClicked);// 移除事件侦听
  260.                                                 sunContainer.removeChild(fallingSun);// 移出显示列表
  261.                                         }
  262.                                 }
  263.                         }
  264.                         //
  265.                         // 安置植物
  266.                         //
  267.                         if (playerMoving) {
  268.                                 movingPlant.x=mouseX;
  269.                                 movingPlant.y=mouseY;
  270.                                 var plantRow:int=Math.floor((mouseY-80)/75);
  271.                                 var plantCol:int=Math.floor((mouseX-25)/65);
  272.                                 // 检查是否在游戏区域内
  273.                                 if (plantRow>=0&&plantCol>=0&&plantRow<5&&plantCol<9) {
  274.                                         selector.visible=true;// 显示选择器
  275.                                         selector.x=25+plantCol*65;
  276.                                         selector.y=80+plantRow*75;
  277.                                 } else {
  278.                                         selector.visible=false;//隐藏选择器
  279.                                 }
  280.                         }
  281.                 }
  282.         }
  283. }
复制代码
如果你一路跟随着我之前的几个教程做了下来的说话,你就会注意到我改变了很多的代码,但是原来的概念保持不变。

我会把注意力放到新的东西上。首先,创建了一个新的数组,名为zombiesArray(第18行),它会存储每一行上的僵尸。这是非常有用的当我们想要知道一株植物是否能开火时。

此刻,只要有一个僵尸与植物处于同一行时植物就能射击,我并不关心僵尸是在植物的左边还是右边,但是这个地方我将会再下一个步骤来改变它,使得只有僵尸在植物

的右边时,植物才能开火,因为我可不想把植物放在僵尸的右边,然后看着它往右没有目标地开火。

当一个新的僵尸被加到舞台,看一下newZombie函数的这两行:
zombie.zombieRow=Math.floor(Math.random()*5);
zombiesArray[zombie.zombieRow]++;

给僵尸声明一个zombieRow属性,然后增加zombiesArray的第zombieRow个元素的值。多亏了这个数组,使得我总是可以轻易地知道有每一行分别有多少僵尸。

再看下placePlant函数(第170行),这个函数我用来放置一株植物,我将给植物增加一些属性:

placedPlant.fireRate=75;
placedPlant.recharge=0;
placedPlant.isFiring=false;
placedPlant.plantRow=plantRow;
我定义了植物开发的速率,单位是帧。它意味着植物至少要每隔fireRate帧开火一次。我选择用帧而不是用毫秒来作为植物开火的速率是因为如果有太多的对象在舞台上

,游戏循环速率就会变慢,而不是保持不变。recharge属性每秒都帧都会增加,当它等于fireRate时,植物就准备好射击了。isFiring属性告诉我们植物是否正在射击。

plantRow属性存储自己所在的行数。当检查有多少僵尸处于这一行时这个属性是很有用的。

来看一下是否一株植物可以开火,我们用这个语句来描述:
if (zombiesArray[currentPlant.plantRow]>0&&currentPlant.recharge==currentPlant.fireRate&&! currentPlant.isFiring) {
        var bullet:bulletMc=new bulletMc();
        bulletContainer.addChild(bullet);
        bullet.x=currentPlant.x;
        bullet.y=currentPlant.y;
        bullet.sonOf=currentPlant;
        currentPlant.recharge=0;
        currentPlant.isFiring=true;
}
这个if语句检查与植物是否至少有一个僵尸与植物同一行,并且植物是否正在开火,是否准备好开火了。如果满足这些条件了,一个新的子弹就被创造出来了,并且植物

的一些状态就被改变了,来告诉脚本植物正在开火,并且需要重新准备。但是所有上面的这些功能的实现,我们需要给子弹一个sonOf属性来存储它是哪一株植物射出来的

。这是非常有用的一旦子弹要被移出舞台,我们就要更新射出该子弹的植物的isFiring状态。

最后看一下杀死僵尸部分的核心代码,它是一个for循环:
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;
                bulletContainer.removeChild(movingBullet);
                if (movingZombie.alpha<0) {
                        zombiesArray[movingZombie.zombieRow]--;
                        zombieContainer.removeChild(movingZombie);
                }
                break;
        }
}
我遍历所有的僵尸,进行碰撞检测。如果一个僵尸被子弹击中,我们移除子弹,并设置射出该子弹的植物的isFiring为false,并且减少僵尸的能量。此刻通过降低僵尸的

透明度。如果透明度降至0,我们移除该僵尸,并且减少zombiesArray相应位置元素的值来更新在该行上僵尸的数量。

现在你可以测试一下游戏了:


收集硬币,购买植物然后杀死僵尸吧。
下载源代码
下一次,我们将会使得僵尸也能攻击。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值