使用FLEX和 Actionscript 开发FLASH游戏(六)-5
使用
FLEX和
Actionscript开发
FLASH 游戏
-碰撞检测(第五页)
Player.as
package
{
import flash.events.*;
import flash.geom.*;
import mx.core.*;
public class Player extends GameObject
{
protected static const TimeBetweenShots:Number=0.25;
protected var shooting:Boolean=false;
protected var timeToNextShot:Number=0;
public function Player()
{
}
public function startupPlayer():void
{
startupGameObject(ResourceManager.BrownPlaneGraphics,new Point (Application.application.width/2,Application.application.height/2),
ZOrders.PlayerZOder);
shooting=flase;
timeToNextShot=0;
this.collisionName=CollisionIdentifiers.PLAYER;
}
override public function shutdown():void
{
super.shutdown();
}
override public function enterFrame(dt:Number):void
{
super.enterFrame(dt);
timeToNextShot-=dt;
if(timeToNextShot<=0&&shooting)
{
timeToNextShot=TimeBetweenShots;
var weapon:Weapon=Weapon.pool.ItemFromPool as Weapon;
weapon.startupBasicWeapon(ResourceManager.TwoBulletsGraphics,
new Point(position.x+graphics.bitmap.width/2- ResourceManager.TwoBulletsGraphics.bitmap.width/2,position.y- graphics.bitmap.height+ResourceManager.TwoBulletsGraphics.bitmap.height*2),
150,
true);
}
}
override public function mouseMove(event:MouseEvent):void
{
//move player to mouse position
position.x=event.stageX;
position.y=event.stageY;
//keep player on the screen
if(position.x<0)
position.x=0;
if(position.x>Application.application.width-graphics.bitmap.width)
position.x=Application.application.width-graphics.bitmap.width;
if(position.y<0)
position.y=0;
if(position.y>Application.application.height-graphics.bitmap.height)
position.y=Application.application.height-graphics.bitmap.height;
}
override public function mouseDown(event:MouseEvent):void
{
shooting=true;
}
override public function mouseUp(event:MouseEvent):void
{
shooting=false;
}
override public function collision(other:GameObject):void
{
Level.Instance.levelEnd=true;
this.shutdown();
}
}
}
如你所见,在游戏者类中做了两处改变来适应新的碰撞检测系统。第一处是在 startup 函数里我们设置了 collisionName(碰撞名 )。第二处是增加了碰撞函数,当检测到碰撞时会被 GameObjectManager类调用。这里我们注意到 Level类中 level应该通过设置 levelEnd为真而结束(因为游戏者被杀死了),我们通过调用 shutdown函数来把游戏者从游戏中删除。
敌机类和武器类中做的改变基本上是一样的,除了在碰撞函数中的修改 Level类的 levelEnd属性有点不同。为了简洁的缘故我不给出敌机类和武器类的代码——你能够在本篇的结尾处下载源代码自己做个比较。
我们必须做的最后的改变是 Level类。现在让我们看看它有哪些改变。
<Previous Page | page: 1 2 3 4 5 6 | Next Page>
Player.as
package
{
import flash.events.*;
import flash.geom.*;
import mx.core.*;
public class Player extends GameObject
{
protected static const TimeBetweenShots:Number=0.25;
protected var shooting:Boolean=false;
protected var timeToNextShot:Number=0;
public function Player()
{
}
public function startupPlayer():void
{
startupGameObject(ResourceManager.BrownPlaneGraphics,new Point (Application.application.width/2,Application.application.height/2),
ZOrders.PlayerZOder);
shooting=flase;
timeToNextShot=0;
this.collisionName=CollisionIdentifiers.PLAYER;
}
override public function shutdown():void
{
super.shutdown();
}
override public function enterFrame(dt:Number):void
{
super.enterFrame(dt);
timeToNextShot-=dt;
if(timeToNextShot<=0&&shooting)
{
timeToNextShot=TimeBetweenShots;
var weapon:Weapon=Weapon.pool.ItemFromPool as Weapon;
weapon.startupBasicWeapon(ResourceManager.TwoBulletsGraphics,
new Point(position.x+graphics.bitmap.width/2- ResourceManager.TwoBulletsGraphics.bitmap.width/2,position.y- graphics.bitmap.height+ResourceManager.TwoBulletsGraphics.bitmap.height*2),
150,
true);
}
}
override public function mouseMove(event:MouseEvent):void
{
//move player to mouse position
position.x=event.stageX;
position.y=event.stageY;
//keep player on the screen
if(position.x<0)
position.x=0;
if(position.x>Application.application.width-graphics.bitmap.width)
position.x=Application.application.width-graphics.bitmap.width;
if(position.y<0)
position.y=0;
if(position.y>Application.application.height-graphics.bitmap.height)
position.y=Application.application.height-graphics.bitmap.height;
}
override public function mouseDown(event:MouseEvent):void
{
shooting=true;
}
override public function mouseUp(event:MouseEvent):void
{
shooting=false;
}
override public function collision(other:GameObject):void
{
Level.Instance.levelEnd=true;
this.shutdown();
}
}
}
如你所见,在游戏者类中做了两处改变来适应新的碰撞检测系统。第一处是在 startup 函数里我们设置了 collisionName(碰撞名 )。第二处是增加了碰撞函数,当检测到碰撞时会被 GameObjectManager类调用。这里我们注意到 Level类中 level应该通过设置 levelEnd为真而结束(因为游戏者被杀死了),我们通过调用 shutdown函数来把游戏者从游戏中删除。
敌机类和武器类中做的改变基本上是一样的,除了在碰撞函数中的修改 Level类的 levelEnd属性有点不同。为了简洁的缘故我不给出敌机类和武器类的代码——你能够在本篇的结尾处下载源代码自己做个比较。
我们必须做的最后的改变是 Level类。现在让我们看看它有哪些改变。
<Previous Page | page: 1 2 3 4 5 6 | Next Page>