?http://bbs.9ria.com/thread-81770-1-5.html
查看该系列的其它文章: 做一个像植物大战僵尸的Flash游戏1 做一个像植物大战僵尸的Flash游戏2 做一个像植物大战僵尸的Flash游戏3 做一个像植物大战僵尸的Flash游戏4 做一个像植物大战僵尸的Flash游戏5
现在我们来到了这个系列教程的第三步。在这部分中,我们我会安置所买的植物,并且让僵尸出场。
增加一个僵尸是相当的容易因为处理僵尸问题跟处理阳光问题一样。就像阳光出现在舞台的顶部之外,然后落下。僵尸出现在舞台右边之外,然后往左移动。然而他们之
间不会相互配合。
至于安置植物,我们必须确保当玩家拖动植物,然后按下鼠标时,植物将要被安置的区块必须没有其它植物并且在游戏区域内。然后,植物就被安置好了,选择器和侦听
器也随之被移除。接着,玩家继续收集阳光并且选择一种植物(现在只有一种)安置到游戏区域里。
我对代码进行了注释来帮助你明白代码都做了些什么。
- package {
- ? ? ? ? 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维数组用来存储游戏区块
- ? ? ? ? ? ? ? ? private var gameField:Array;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //计时器,使得阳光落下
- ? ? ? ? ? ? ? ? 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();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //所有僵尸的容器
- ? ? ? ? ? ? ? ? private var zombieContainer:Sprite=new Sprite();
- ? ? ? ? ? ? ? ? //阳光
- ? ? ? ? ? ? ? ? private var sun:sunMc;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //植物栏上的植物
- ? ? ? ? ? ? ? ? private var plant:plantMc;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //玩家在游戏区域能够拖动的植物
- ? ? ? ? ? ? ? ? private var movingPlant:plantMc;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //选择器(一个高亮的区块),告诉玩家他将把植物种在哪个区块
- ? ? ? ? ? ? ? ? private var selector:selectorMc;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //玩家所拥有的金钱数量
- ? ? ? ? ? ? ? ? private var money:uint=0;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //动态文本框,用来显示玩家的金钱
- ? ? ? ? ? ? ? ? private var moneyText:TextField=new TextField??;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //布尔型变量,标志玩家是否在移动一个植物
- ? ? ? ? ? ? ? ? private var playerMoving:Boolean=false;
- ? ? ? ? ? ? ? ? public function Main():void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //初始化游戏区块
- ? ? ? ? ? ? ? ? ? ? ? ? setupField();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //画出游戏区块
- ? ? ? ? ? ? ? ? ? ? ? ? drawField();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //处理下落的阳光
- ? ? ? ? ? ? ? ? ? ? ? ? fallingSuns();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //添加所有文本(此刻只有金钱的数量)
- ? ? ? ? ? ? ? ? ? ? ? ? addText();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //增加植物的函数
- ? ? ? ? ? ? ? ? ? ? ? ? addPlants();
- ? ? ? ? ? ? ? ? ? ? ? ? //增加僵尸的函数
- ? ? ? ? ? ? ? ? ? ? ? ? addZombies();
- ? ? ? ? ? ? ? ? ? ? ? ? //注册侦听器,进入游戏循环
- ? ? ? ? ? ? ? ? ? ? ? ? addEventListener(Event.ENTER_FRAME,onEnterFrm);
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function setupField():void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //创建一个5行9列的矩阵,并将矩阵内的每个值初始化为0
- ? ? ? ? ? ? ? ? ? ? ? ? gameField=new Array();
- ? ? ? ? ? ? ? ? ? ? ? ? for (var i:uint=0; i<5; i++) {
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? gameField[i]=new Array();
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (var j:uint=0; j<9; j++) {
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? gameField[i][j]=0;
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function addText():void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //增加一个动态文本框
- ? ? ? ? ? ? ? ? ? ? ? ? addChild(moneyText);
- ? ? ? ? ? ? ? ? ? ? ? ? updateMoney();
- ? ? ? ? ? ? ? ? ? ? ? ? moneyText.textColor=0xFFFFFF;
- ? ? ? ? ? ? ? ? ? ? ? ? moneyText.height=20;
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function updateMoney():void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把玩家所拥有的金钱数量写到文本框里
- ? ? ? ? ? ? ? ? ? ? ? ? moneyText.text="Money: "+money.toString();
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? 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);
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function addZombies():void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //增加僵尸容器
- ? ? ? ? ? ? ? ? ? ? ? ? addChild(zombieContainer);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //启动计时器,使僵尸出场
- ? ? ? ? ? ? ? ? ? ? ? ? zombieTimer.start();
- ? ? ? ? ? ? ? ? ? ? ? ? //注册侦听器
- ? ? ? ? ? ? ? ? ? ? ? ? zombieTimer.addEventListener(TimerEvent.TIMER,newZombie);
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function newZombie(e:TimerEvent):void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //创建一个僵尸
- ? ? ? ? ? ? ? ? ? ? ? ? var zombie:zombieMc=new zombieMc();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把僵尸加入到显示列表
- ? ? ? ? ? ? ? ? ? ? ? ? zombieContainer.addChild(zombie);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //生成随机行数,用于放置僵尸
- ? ? ? ? ? ? ? ? ? ? ? ? var row:uint=Math.floor(Math.random()*5);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把僵尸放在屏幕的右边
- ? ? ? ? ? ? ? ? ? ? ? ? zombie.x=660;
- ? ? ? ? ? ? ? ? ? ? ? ? zombie.y=row*75+115;
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function fallingSuns():void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //增加阳光容器
- ? ? ? ? ? ? ? ? ? ? ? ? addChild(sunContainer);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //启动计时器,使得阳光落下
- ? ? ? ? ? ? ? ? ? ? ? ? flowersTimer.start();
- ? ? ? ? ? ? ? ? ? ? ? ? //注册Timer事件,触发事件侦听函数
- ? ? ? ? ? ? ? ? ? ? ? ? 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);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //创建阳光
- ? ? ? ? ? ? ? ? ? ? ? ? sun = new sunMc();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //当鼠标滑过阳光时,改变鼠标的形状
- ? ? ? ? ? ? ? ? ? ? ? ? sun.buttonMode=true;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把阳光加入到显示列表
- ? ? ? ? ? ? ? ? ? ? ? ? sunContainer.addChild(sun);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把阳光放在合适的位置
- ? ? ? ? ? ? ? ? ? ? ? ? sun.x=52+sunCol*65;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //定义阳光destinationY属性
- ? ? ? ? ? ? ? ? ? ? ? ? sun.destinationY=130+sunRow*75;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把阳光放在舞台顶部的上方
- ? ? ? ? ? ? ? ? ? ? ? ? sun.y=-20;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //给阳光注册鼠标点击事件
- ? ? ? ? ? ? ? ? ? ? ? ? sun.addEventListener(MouseEvent.CLICK,sunClicked);
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function sunClicked(e:MouseEvent):void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //移除鼠标事件侦听
- ? ? ? ? ? ? ? ? ? ? ? ? e.currentTarget.removeEventListener(MouseEvent.CLICK,sunClicked);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //让玩家赚到5个金币
- ? ? ? ? ? ? ? ? ? ? ? ? money+=5;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //更新动态文本
- ? ? ? ? ? ? ? ? ? ? ? ? updateMoney();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //获得我们必须移除的阳光
- ? ? ? ? ? ? ? ? ? ? ? ? var sunToRemove:sunMc=e.currentTarget as sunMc;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //移除该阳光
- ? ? ? ? ? ? ? ? ? ? ? ? sunContainer.removeChild(sunToRemove);
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function addPlants():void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //增加植物容器
- ? ? ? ? ? ? ? ? ? ? ? ? addChild(plantContainer);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //创建一个新的植物
- ? ? ? ? ? ? ? ? ? ? ? ? plant=new plantMc();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把新植物加入到显示列表
- ? ? ? ? ? ? ? ? ? ? ? ? plantContainer.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();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //创建一个新的选择器
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? selector=new selectorMc();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //使选择器不可见
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? selector.visible=false;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把选择器加入到显示列表
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? plantContainer.addChild(selector);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //构建一个新的供玩家拖动的植物
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? movingPlant=new plantMc();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //给该植物注册一个鼠标点击事件
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? movingPlant.addEventListener(MouseEvent.CLICK,placePlant);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把该植物加入到显示列表
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? plantContainer.addChild(movingPlant);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //告诉脚本正在移动一株植物
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? playerMoving=true;
- ? ? ? ? ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function placePlant(e:MouseEvent):void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //根据鼠标的位置来判断植物将要被种植在哪个区块
- ? ? ? ? ? ? ? ? ? ? ? ? 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&&gameField[plantRow][plantCol]==0) {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???//构建一株植物,用来种植
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var placedPlant:plantMc=new plantMc();
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把该植物加入到显示列表
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? plantContainer.addChild(placedPlant);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //放置该植物
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? placedPlant.x=plantCol*65+57;
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? placedPlant.y=plantRow*75+115;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //告诉脚本玩家停止移动植物了
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? playerMoving=false;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //移除供玩家拖动的植物的事件侦听
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? movingPlant.removeEventListener(MouseEvent.CLICK,placePlant);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把选择器从显示列表移除
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? plantContainer.removeChild(selector);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //把供玩家拖动的植物移出显示列表
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? plantContainer.removeChild(movingPlant);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //更新游戏区域信息
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? gameField[plantRow][plantCol]=1;
- ? ? ? ? ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? private function onEnterFrm(e:Event):void {
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //循环,遍历所有的僵尸
- ? ? ? ? ? ? ? ? ? ? ? ? for (var i:uint=0; i<zombieContainer.numChildren; i++) {
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var movingZombie:zombieMc=zombieContainer.getChildAt(i) as zombieMc;
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???//把所有的僵尸向左移动二分之一个像素
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? movingZombie.x-=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);
- ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???//如果这个区块在游戏区域内
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 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;
- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? ? ? ? ? }
- ? ? ? ? ? ? ? ? }
- ? ? ? ? }
- }
复制代码
下面是结果:
收集阳光并且安置植物当僵尸到达时。
下载源代码。下一次,我们将会与僵尸作战! 译者注: 有人问,为什么不面象对象呢?为什么把代码全部写到一个类里,这多少不容易扩展和维护啊。 恩。这个问题你得问原作者了。因为我这只是翻译。 但是,我倒是可以理解。原作者这么写,但是他把所有主要的东西都写出来了。当你要具体把这个游戏完整的做出来的时候,你得靠自己来设计,来架构。显然原作者是目的并不是教授游戏设计,游戏架构的。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|