FLEX和Actionscript开发FLASH游戏5-1

FLEX和Actionscript开发FLASH游戏5-1
2010年11月12日
  使用FLEX和Actionscript开发FLASH 游戏-增加武器
  本系列到现在已经实现了足够的底层代码使得为游戏增加新的元素非常的容易。通过使用GameObjectManager和GameObject类处理绘制和更新游戏元素的工作以及用Level类实际生成新的元素,实现新的游戏元素就仅仅只需要很少量的代码了。我们将利用这个来给游戏增加一些敌机,并且给游戏者增加战斗用的武器。
  首先,让我们看看在Level类中做的一些改变。
  Level.as
  package
  {
  import flash.events.*;
  import flash.geom.*;
  import flash.media.*;
  import flash.net.*;
  import flash.utils.*;
  import mx.collections.ArrayCollection;
  import mx.core.*;
  public class Level
  {
  protected static var instance:Level=null;
  protected static const TimeBetweenLevelElements:Number=2;
  protected static const TimeBetweenEnemies:Number=3;
  protected static const TimeBetweenClouds:Number=2.5;
  protected var timeToNextLevelElement:Number=0;
  protected var levelElementGraphics:ArrayCollection=new ArrayCollection();
  protected var timeToNextEnemy:Number=0;
  protected var enemyElementGraphics:ArrayCollection=new ArrayCollection();
  protected var timeToNextCloud:Number=0;
  static public function get Instance():Level
  {
  if(instance==null)
  instance=new Level();
  return instance;
  }
  public function Level(caller:Function=null)
  {
  if(Level.instance!=null)
  throw new Error("Only one Singleton instance should be instantiated");
  levelElementGraphics.addItem(ResourceManager.Small IslandGraphics);
  levelElementGraphics.addItem(ResourceManager.BigIs landGraphics);
  levelElementGraphics.addItem(ResourceManager.Volca noIslandGraphics);
  enemyElementGraphics.addItem(ResourceManager.Small BluePlaneGraphics);
  enemyElementGraphics.addItem(ResourceManager.Small GreenPlaneGraphics);
  enemyElementGraphics.addItem(ResourceManager.Small WhitePlaneGraphics);
  }
  public function startup():void
  {
  timeToNextLevelElement=0;
  new Player().startupPlayer();
  }
  public function shutdown():void
  {
  }
  public function enterFrame(dt:Number):void
  {
  //add a background element
  timeToNextLevelElement-=dt;
  if(timeToNextLevelElement<=0)
  {
  timeToNextLevelElement=TimeBetweenLevelElements;
  var graphics:GraphicsResource=levelElementGraphics.get ItemAt (MathUtils.randomInteger(0,levelElementGraphics.le ngth)) as GraphicsResource;
  var backgroundLevelElement:BackgroundLevelElement=
  BackgroundLevelElement.pool.ItemFromPool as BackgroundLevelElement
  backgroundLevelElement.startupBackgroundLevelEleme nt(graphics,new Point (Math.random()*Application.applilcation.width, -ZOrders.BackgroundZOrder,50); } //add an enemy timeToNextEnemy-=dt; if(timeToNextEnemy<=0) { timeToNextEnemy=TimeBetweenEnemies; var enemygraphics:GraphicsResource=enemyElementGraphic s.getItemAt (MathUtils.randomInteger(0,enemyElementGraphics.le ngth))as GrahpicsResource; var enemy:Enemy=Enemy.pool.ItemFromPool as Enemy; enemy.startupBasicEnemy( new Point(Math.random()*Application.application.width, - enemygraphics.bitmap.height),55); } //add cloud timeToNextCloud-=dt; if(timeToNextCloud<=dt) { timeToNextCloud=TimeBetweenClouds; var cloudBackgroundLevelElement:BackgroundLevelElement = BackgroundLevelElement.pool.ItemFromPool as BackgroundLevelElement; cloudBackgroundLevelElement.startupBackgroundLevel Element( ResourceManager.CloudGraphics,new Point(Math.random() *Application.application.width,- ResourceManager.CloudGraphics.bitmap.height),ZOrde rs.CloudsBelowZOrder,75); } } } } timeToNextEnemy/TimeBetweenEnemies 和timeToNextCloud/TimeBetweenClouds属性对和timeToNextLevelElement/TimeBetweenLevelElements 属性对(在第四部分详细描述的)是一样的作用,除了它们是各自用来增加敌机和云朵。enemyElementGraphics属性同levelElementGraphics(也在第四部分详细描述过)是一样的作用,除了这里它用来提供生成敌机的图像资源。
  我们还在enterFrame函数里增加了两个新的代码块来生成敌机和云朵。这些代码几乎和生成BackgroundLevelElement的代码块完全一样。
  云朵是作为一个BackgroundLevelElement来实现的,除了使用了稍快些的滚动速度和更高级别的zOrder。这让它们看起来处在更高的海拔高度。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值