SWF植物大战僵尸2

现在,我们来到了这个教程的第5个阶段。
在这个阶段,我们会修改一个漏洞,这个漏洞允许植物开火只要有僵尸与植物处于同一行,不论僵尸是在植物的左边还是右边。同时我们也会增加僵尸攻击植物的功能。
首先,让我来解释一下一些改变:
在前面的步骤里,zombiesArray数组只存储每一行上的僵尸的数量。这个信息对我们想知道僵尸是在植物的左边还是右边是不够的,所以从现在开始zombiesArray改为一个二维数组,用来存储每一行上僵尸的名字。
你将会明白这一特性当我们讨论源代码的时候。
要使僵尸攻击植物,我们必须使他们停下来一旦他们与植物处于同一区块。
让我们来看源代码:
import flash.display.S
import flash.utils.T
import flash.events.TimerE
import flash.events.MouseE
import flash.events.E
import flash.text.TextF
public class Main extends Sprite {
// 一个2维数组用来存储游戏区块
private var plantsArray:A// 种植在游戏区域里的植物
private var zombiesArray:A//在游戏区域里的僵尸
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;//
玩家在游戏区域能够拖动的植物
private var selector:selectorMc;// 选择器(一个高亮的区块),告诉玩家他将把植物种在哪
// 其它变量
private var money:uint=0;// 玩家所拥有的金钱数量
private var moneyText:TextField=new TextF// 动态文本框,用来显示玩家的金钱
private var playerMoving:Boolean=// 布尔型变量,标志玩家是否在移动一个植物
private var totalZombies:uint=0;//僵尸的总数
public function Main():void {
setupField();//初始化游戏区块
drawField();//画出游戏区块
fallingSuns();// 初始化下落的阳光
addPlants();// 初始化植物
addZombies();//初始化僵尸
addEventListener(Event.ENTER_FRAME,onEnterFrm);
游戏区域设置,创建用来存储植物和僵尸信息的数组
private function setupField():void {
plantsArray=new Array();
zombiesArray=new Array();
for (var i:uint=0; i&5; i++) {
plantsArray[i]=new Array();
zombiesArray[i]=new Array();
for (var j:uint=0; j&9; j++) {
plantsArray[i][j]=0;
// 显示玩家的金钱
private function updateMoney():void {
moneyText.text=&Money: &+money.toString();
// 画出游戏区域
private function drawField():void {
var fieldSprite:Sprite=new Sprite();
var randomGreen:N
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();// 构造僵尸
totalZombies++;
zombieContainer.addChild(zombie);// 增加僵尸
zombie.zombieRow=Math.floor(Math.random()*5);//生成随机行数,用于放置僵尸
zombie.name=&zombie_&+totalZ//给僵尸一个名字
zombiesArray[zombie.zombieRow].push(zombie.name);// 增加第row行的僵尸
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();// 构造阳光
sun.buttonMode=// 当鼠标滑过阳光时,改变鼠标的形状
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();// 更新动态文本
var sunToRemove:sunMc=e.currentTarget as sunMc;// 获得我们必须移除的阳光
sunContainer.removeChild(sunToRemove);// 移除该阳光
// 创建一个植物栏,现在只有一种植物
private function addPlants():void {
var plant:plantMc=new plantMc();// 构造一株新的植物
overlayContainer.addChild(plant);// 增加植物
plant.buttonMode=// 使鼠标改变形状,当它滑过新植物时
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();// 更新动态文本
selector=new selectorMc();// 创建一个新的选择器
selector.visible=// 使选择器不可见
overlayContainer.addChild(selector);// 把选择器加入到显示列表
movingPlant=new plantMc();// 构建一个新的供玩家拖动的植物
movingPlant.addEventListener(MouseEvent.CLICK,placePlant);// 给该植物注册一个鼠标点击事件
overlayContainer.addChild(movingPlant);// 把该植物加入到显示列表
playerMoving=// 告诉脚本正在移动一株植物
// 把植物放置在游戏区域中
private function placePlant(e:MouseEvent):void {
var plantRow:int=Math.floor((mouseY-80)/75);
var plantCol:int=Math.floor((mouseX-25)/65);
// let's see if the tile is inside the game field and it's free
if (plantRow&=0&&plantCol&=0&&plantRow&5&&plantCol&9&&plantsArray[plantRow][plantCol]==0) {
var placedPlant:plantMc=new plantMc();// 构建一株植物,用来种植
placedPlant.name=&plant_&+plantRow+&_&+plantC// 给植物一个名字
placedPlant.fireRate=75;// 植物的开火速率,单位帧
placedPlant.recharge=0;// 当recharge 等于 fireRate时,植物已经准备好开火了
placedPlant.isFiring=// 一个布尔变量来存储植物是否正在开火
placedPlant.plantRow=plantR// 植物所在的行
plantContainer.addChild(placedPlant);// 把该植物加入到显示列表
placedPlant.x=plantCol*65+57;
placedPlant.y=plantRow*75+115;
playerMoving=// 告诉脚本玩家不在移动植物了
movingPlant.removeEventListener(MouseEvent.CLICK,placePlant);// 移除事件侦听
overlayContainer.removeChild(selector);// 移除选择器
overlayContainer.removeChild(movingPlant);// 移除供拖动的植物
plantsArray[plantRow][plantCol]=1;//
更新游戏区块信息
// 游戏循环,游戏的核心函数
private function onEnterFrm(e:Event):void {
// 植物管理
for (i=0; i&plantContainer.numC i++) {
var currentPlant:plantMc=plantContainer.getChildAt(i) as plantMc;
// 让我们看看植物是否能开火
if (currentPlant.recharge==currentPlant.fireRate&&! currentPlant.isFiring) {
// 检查是否有僵尸与植物处于同一行
if (zombiesArray[currentPlant.plantRow].length&0) {
// 遍历僵尸
for (j=0; j&zombiesArray[currentPlant.plantRow].length&0; j++) {
var targetZombie:zombieMc=zombieContainer.getChildByName(zombiesArray[currentPlant.plantRow]
[j]) as zombieMc;// 获得第j个僵尸
// 如果僵尸在植物的右边
if (targetZombie.x¤tPlant.x) {
var bullet:bulletMc=new bulletMc();// 创建一个新子弹
bulletContainer.addChild(bullet);// 加入到显示列表
bullet.x=currentPlant.x;
bullet.y=currentPlant.y;
bullet.sonOf=currentP// 存储该子弹是由哪一株植物射出的
currentPlant.recharge=0;// 重新准备开火
currentPlant.isFiring=// 植物正在开火
// 终止for循环
if (currentPlant.recharge¤tPlant.fireRate) {
currentPlant.recharge++;
// 子弹管理
for (i=0; i&bulletContainer.numC 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=// 植物不再处于正在开火的状态
bulletContainer.removeChild(movingBullet);// 移除子弹
for (j=0; j&zombieContainer.numC j++) {
var movingZombie:zombieMc=zombieContainer.getChildAt(j) as zombieMc;
// 让我们看看植物是否被子弹击中
if (movingZombie.hitTestPoint(movingBullet.x,movingBullet.y,true)) {
movingZombie.alpha-=0.3;//减少僵尸的能量(透明度)
firingPlant.isFiring=// 植物不再处于正在开火的状态
bulletContainer.removeChild(movingBullet);// 移除子弹
// 让我们看看僵尸的能量(透明度)是否降至为0了
if (movingZombie.alpha&0) {
zombiesArray[movingZombie.zombieRow].splice(zombiesArray
[movingZombie.zombieRow].indexOf(movingZombie.name),1);// 减少该行的僵尸
zombieContainer.removeChild(movingZombie);// 移除显示列表
// 僵尸管理
var zombieColumn:
for (i=0; i&zombieContainer.numC i++) {
movingZombie=zombieContainer.getChildAt(i) as zombieMc;
zombieColumn = Math.floor((movingZombie.x-25)/65);// 得到僵尸所在的列
// 检查是否有植物与之处于同一个区块
if (zombieColumn&0||zombieColumn&8||plantsArray[movingZombie.zombieRow][zombieColumn]==0) {
movingZombie.x-=0.5;// 每一个僵尸往左移动0.5个像素
// 僵尸开始攻击!!
var attackedPlant:plantMc=plantContainer.getChildByName(&plant_&+movingZombie.zombieRow+&_&+zombieColumn) as
attackedPlant.alpha-=0.01;// drains plant energy
//检查植物是否死了
if (attackedPlant.alpha&0) {
plantsArray[movingZombie.zombieRow][zombieColumn]=0;//把植物移出数组
plantContainer.removeChild(attackedPlant);//移出显示列表
// 阳光管理
for (i=0; i&sunContainer.numC i++) {
var fallingSun:sunMc=sunContainer.getChildAt(i) as sunMc;
// 让我们看看阳光是否还在下落
if (fallingSun.y&fallingSun.destinationY) {
fallingSun.y++;// 把阳光往下移动一个像素
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);
// 检查是否在游戏区域内
if (plantRow&=0&&plantCol&=0&&plantRow&5&&plantCol&9) {
selector.visible=// 显示选择器
selector.x=25+plantCol*65;
selector.y=80+plantRow*75;
selector.visible=// 隐藏选择器
在setupField函数中,zombiesArray变成了一个二维数组,一个元素代表一行:
private function setupField():void {
& && &&&plantsArray=new Array();
& && &&&zombiesArray=new Array();
& && &&&for (var i:uint=0; i&5; i++) {
& && && && && & plantsArray=new Array();
& && && && && & zombiesArray=new Array();
& && && && && & for (var j:uint=0; j&9; j++) {
& && && && && && && && &plantsArray[j]=0;
& && && && && & }
在newZombie函数中,我们根据僵尸所在的行,把僵尸的名字加入到zombiesArray的合适的位置:
private function newZombie(e:TimerEvent):void {
& && &&&var zombie:zombieMc=new zombieMc();// 构造僵尸
& && &&&totalZombies++;
& && &&&zombieContainer.addChild(zombie);// 加入到显示列表
& && &&&zombie.zombieRow=Math.floor(Math.random()*5);// 选择一个随机行来安置僵尸
& && &&&zombie.name=&zombie_&+totalZ//给僵尸一个名字
& && &&&zombiesArray[zombie.zombieRow].push(zombie.name);// 增加第row行的僵尸
& && &&&zombie.x=660;// 把僵尸放在舞台右边
& && &&&zombie.y=zombie.zombieRow*75+115;
现在,整个判断一株植物是否能开火就被改成了这样:
if (currentPlant.recharge==currentPlant.fireRate&&! currentPlant.isFiring) {
& && &&&if (zombiesArray[currentPlant.plantRow].length&0) {
& && && && && & for (j=0; j&zombiesArray[currentPlant.plantRow].length&0; j++) {
& && && && && && && && &var targetZombie:zombieMc=zombieContainer.getChildByName(zombiesArray[currentPlant.plantRow][j]) as zombieMc;
& && && && && && && && &if (targetZombie.x¤tPlant.x) {
& && && && && && && && && && &&&var bullet:bulletMc=new bulletMc();
& && && && && && && && && && &&&bulletContainer.addChild(bullet);
& && && && && && && && && && &&&bullet.x=currentPlant.x;
& && && && && && && && && && &&&bullet.y=currentPlant.y;
& && && && && && && && && && &&&bullet.sonOf=currentP
& && && && && && && && && && &&¤tPlant.recharge=0;
& && && && && && && && && && &&¤tPlant.isFiring=
& && && && && && && && && && &&&
& && && && && && && && &}
& && && && && & }
我们遍历了所有与植物处于同一行上的僵尸,然后检查僵尸是在植物的左边还是在植物的右边(第214行)。
下面几行简单的代码使得僵尸攻击植物:
var zombieColumn:
for (i=0; i&zombieContainer.numC i++) {
& && &&&movingZombie=zombieContainer.getChildAt(i) as zombieMc;
& && &&&zombieColumn = Math.floor((movingZombie.x-25)/65);
& && &&&if (zombieColumn&0||zombieColumn&8||plantsArray[movingZombie.zombieRow][zombieColumn]==0) {
& && && && && & movingZombie.x-=0.5;
& && &&&} else {
& && && && && & var attackedPlant:plantMc=plantContainer.getChildByName(&plant_&+movingZombie.zombieRow+&_&+zombieColumn) as plantMc;
& && && && && & attackedPlant.alpha-=0.01;
& && && && && & if (attackedPlant.alpha&0) {
& && && && && && && && &plantsArray[movingZombie.zombieRow][zombieColumn]=0;
& && && && && && && && &plantContainer.removeChild(attackedPlant);
& && && && && & }
我们先检查了是否有植物被安置在僵尸当前所在的区块上,如果有,僵尸就停下脚步然后开始攻击植物,减少植物的生命值(当前是透明度)。
下面是结果:
/wp-content/uploads/2011/03/pvz.swf
本文已收录于以下专栏:
相关文章推荐
/thread-.html
现在,我们来到了这个教程的第5个阶段。
在这个阶段,我们会修改一个漏洞,这个漏洞允许植物开火只要有僵尸与植物处于...
/thread-.html
查看该系列的其它文章:
做一个像植物大战僵尸的Flash游戏1
做一个像植物大战僵尸的Flash游戏2
定义游戏的主要结构
植物大战僵给了我们很好的视觉和感觉上的享受。你得保护你的房子以免被吃脑的僵尸入侵,这相当的吸引人。总的来说,杀死僵尸是很有趣的。但是这视觉上的东西和游戏玩法无关,我们可以用屠...
/thread-.html
查看该系列的其它文章:
做一个像植物大战僵尸的Flash游戏1
做一个像植物大战僵尸的Flash游戏2
/thread-.html
欢迎来到第四步。在这个步骤里,我们会使得植物能够开火,并且最终杀死僵尸。
让我们先来明确一下什么时候植能开火:
...
现在我们来到了这个系列教程的第三步。在这部分中,我们我会安置所买的植物,并且让僵尸出场。
增加一个僵尸是相当的容易因为处理僵尸问题跟处理阳光问题一样。就像阳光出现在舞台的顶部之外,然后落下。僵尸...
第二个教程了,是时候去筹钱买一个植物了。
我们必需修改一个在教程一中的代码,就是让阳光只是简简单单地出现在某一区块中,并且在一段时间内,若没有被捡起,它就消失了。我们得让阳光平滑的落下。
上期已经和大家讲解了做游戏的前提要求,今天要给大家讲述的是游戏界面如何绘制。
一:如何获取到surface:
        (1)首先我们需要获取一个surface,我们先通过s...
植物大战僵尸1几年前曾经风靡一时,妇孺皆知!其续作奇幻时空之旅千呼万唤始出来,不过从首发到目前都1月有余,本人竟然还没玩过。
        于是昨晚下载了一个汉化版。本人系统为IOS5.0.1,ap...
他的最新文章
讲师:王哲涵
讲师:韦玮
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)当前位置:
植物大战僵尸
中文无尽版
更新时间:
软件大小:60.70MB
软件类型:国产软件
软件分类:
软件语言:简体
软件授权:免费软件
支持系统:
  植物大战僵尸无尽版是一个看似简单实则极富策略性的小游戏,可怕的僵尸即将入侵,唯一的防御方式就是栽种的植物。植物大战僵尸无尽版是一个比较特殊的版本,在这个版本中,玩家可以直接点击游戏主界面上益智模式旁边的锁形图标来直接解锁所有内容,可以直接进入无尽模式。
  植物大战僵尸魔幻版功能特点
  1. 独特的五种游戏模式:冒险,小游戏,益智,生存,花园模式;
  2. 多达50个的冒险模式关卡设定,从白天到夜晚,从天台到游泳池,场景变化多样;
  3. 游戏共有26种不同的僵尸敌人,包括开着车子的僵尸驾驶员,游戏更具挑战性;
  4. 49种功能强大,互不相同的植物,并可收集硬币购买宠物蜗牛等多种可选道具;
  5. 打开年历,可以看到各种不同的植物与工具,还有有趣的新闻;
  6. 通过疯狂戴夫店购买特殊植物和工具,以任何你能想象得到的方式干掉僵尸;
  7. 精致的游戏画面与声音,同时还有奖励型的音乐与视频;
  8. 无限次重玩,不会经历两次同样的事件,随时给最新鲜的游戏体验。
  1、下载完成,解压后直接运行.exe文件;
  2、游戏整合最新魔幻版补丁,***完成即可开始游戏。
  操作系统:Windows XP,Windows Vista,Windows 7,Windows 8
  运行环境:无特殊需求
  CPU:Intel Core2 Duo E4600 @ 2.40Hz / AMD Athlon 64 X2 Dual Core 5000+
  内存:2 GB
  显卡:GeForce 8800 GT / Radeon HD 3870
  硬盘:4 GB
植物大战僵尸下载地址
Windows版下载
植物大战僵尸相关版本
精品软件推荐
下载周排行
下载总排行
官方版v4.1.7.7116
官方最新v2.5.3.4138
官方最新版 v3.5.7.5
官方最新版v3.2.1.907
中文免费版
官方免费版
官方最新版
官方中文版
电脑版v1.0.102.816
最新软件专题
最新教程合集
热门关键词
热门专题合集
下载之家是国内最值得信赖的官方软件下载资源提供商,提供安全无毒的绿色软件下载、手机软件下载、游戏下载等。高速安全的软件下载尽在下载之家!
Copyright &
下载之家 (). All Rights Reserved.

参考资料

 

随机推荐