FLEX和Actionscript开发FLASH游戏6-2(1)
2010年11月16日
使用FLEX和Actionscript开发FLASH 游戏-碰撞检测 GameObjectManager.as package { import flash.display.*; import flash.events.*; import flash.utils.*; import mx.collections.*; import mx.core.*; public class GameObjectManager { //double buffer public var backBuffer:BitmapData; //colour to use to clear backbuffer with public var clearColour: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 void adding items //to 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(); protected var collisionMap:Dictionary=new Dictionary(); 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,Applicati on.application.height,false); } public function startup():void { lastFrame=new Date(); } public function shutdown():void { shutdownAll(); } public function enterFrame():void { //Calculate the time since the last frame var thisFrame:Date=new Date(); var seconds:Number=(thisFrame.getTime()-lastFrame.getT ime())/1000.0; lastFrame=thisFrame; removeDeletedGameObjects(); insertNewGameObjects(); Level.Instance.enterFrame(seconds); checkCollisions(); //now allow objects to update themselves for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.enterFrame(seconds); } drawObjects(); } public function click(event:MouseEvent):void { for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.click(event); } } public function mouseDown(event:MouseEvent):void { for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.mouseDown(event); } } public function mouseUp(event:MouseEvent):void { for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.mouseUp(event); } } public function mouseMove(event:MouseMove):void { for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.mouseMove(event); } } 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):void { newGameObjects.addItem(gameObject); } public function removeGameObject(gameObject:GameObject):void { removedGameObjects.addItem(gameObject); }
2010年11月16日
使用FLEX和Actionscript开发FLASH 游戏-碰撞检测 GameObjectManager.as package { import flash.display.*; import flash.events.*; import flash.utils.*; import mx.collections.*; import mx.core.*; public class GameObjectManager { //double buffer public var backBuffer:BitmapData; //colour to use to clear backbuffer with public var clearColour: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 void adding items //to 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(); protected var collisionMap:Dictionary=new Dictionary(); 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,Applicati on.application.height,false); } public function startup():void { lastFrame=new Date(); } public function shutdown():void { shutdownAll(); } public function enterFrame():void { //Calculate the time since the last frame var thisFrame:Date=new Date(); var seconds:Number=(thisFrame.getTime()-lastFrame.getT ime())/1000.0; lastFrame=thisFrame; removeDeletedGameObjects(); insertNewGameObjects(); Level.Instance.enterFrame(seconds); checkCollisions(); //now allow objects to update themselves for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.enterFrame(seconds); } drawObjects(); } public function click(event:MouseEvent):void { for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.click(event); } } public function mouseDown(event:MouseEvent):void { for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.mouseDown(event); } } public function mouseUp(event:MouseEvent):void { for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.mouseUp(event); } } public function mouseMove(event:MouseMove):void { for each(var gameObject:GameObject in gameObjects) { if(gameObject.inuse) gameObject.mouseMove(event); } } 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):void { newGameObjects.addItem(gameObject); } public function removeGameObject(gameObject:GameObject):void { removedGameObjects.addItem(gameObject); }