FLEX和Actionscript开发FLASH游戏 3-2

FLEX和Actionscript开发FLASH游戏 3-2
2010年10月26日
  使用FLEX和Actionscript开发FLASH 游戏-嵌入资源等
  package
  {
  import flash.display.*;
  import mx.collection.*;
  import mx.core.*;
  public class GameObjectManager
  {
  //double buffer
  public var backBuffer:BitmapData;
  //colour to use to clear backbuffer with
  public var clearColor:uint=0xFF0043AB;
  //static instance
  protected static var instance:GameObjectManager=null;
  //the last frame time
  protected var lastFrame:Date;
  //a collection of the GameObjects
  protected var gameObjects:ArrayCollection=new ArrayCollection();
  //a collection where new GameObjects are placed,to avoid adding items
  //a gameObjects while in the gameObjects collection while it is in a loop
  protected var newGameObjects:ArrayCollection = new ArrayCollection();
  //a collection where removed GameObjects are placed,to avoid removing items
  //to gameObjects while in the gameObjects collection while it is in a loop
  protected var removedGameObjects:ArrayCollection=new ArrayCollection();
  static public function get Instance():GameObjectManager
  {
  if(instance == null)
  instance=new GameObjectManager();
  return instance;
  }
  public function GameObjectManager()
  {
  if(instance!=null)
  throw new Error("Only one Singleton instance should be instantiated");
  backBuffer=new BitmapData (Application.application.width,Application.applica tion.height,false);
  }
  public function startup():void
  {
  lastFrame=new Date();
  new Bounce().startupBounce();
  }
  public function shutdown():void
  {
  shutdownAll();
  }
  public function enterFrame():void
  {
  //Calculate the time since the lastframe
  var thisFrame:Date=new Date();
  var seconds:Number=(thisFrame.getTime()-lastFrame.getT ime())/1000.0;
  lastFrame=thisFrame;
  removeDeletedGameObjects();
  insertNewGameObjects();
  //now allow objects to update themselves
  for each(var gameObject:GameObject in gameObjects)
  {
  if(gameObject.inuse)
  gameObject.enterFrame(seconds);
  }
  drawObjects();
  } protected function drawObjects():void { backBuffer.fillRect(backBuffer.rect,clearColor); //draw the objects for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.copyToBackBuffer(backBuffer); } } public function addGameObject(gameObject:GameObject):viod { newGameObjects.addItem(gameObject); } public function removeGameObject(gameObject:GameObject):void { removedGameObjects.addItem(gameObject); } protected function shutdownAll():void { //don't dispose objects twice for each(var gameObject:GameObject in gameObjects) { var found:Boolean=false; for each (var removedObject:GameObject in removedGameObjects) { if(removedObject==gameObject) { found=true; break; } } if (!found) gameObject.shutdown(); } } protected function insertNewGameObjects():void { for each(var gameObject:GameObject in newGameObjects) { for (var i:int=0;igameObject.zOrde r|| gameObjects.getItemAt(i).zOrder==-1) break; } gameObjects.addItemAt(gameObject,i); } newGameObjects.removedAll(); } protected function removedDeletedGameObjects():void { //insert the object acording to it's z position for each(var removedObject:GameObject in removedGameObjects) { var i:int=0; for (i=0;i<gameObjects.length;++i) { if(gameObjects.getItemAt(i)==removedObject) { gameObjects.removeItemAt(i); break; } } } removedGameObjects.removedAll(); } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值