使用FLEX和 Actionscript 开发FLASH游戏(三)-1
2010年07月10日
在第三部分我们将增加一些图像资源并把它们显示在屏幕上
标签: Flex, Flash, tutorial, Actionscript, game
转到: 第一部分
在写一个新程序的时候,总存在这么一个点,从这儿你可以看见你的劳动成果。通过实现状态和绘制的管道,我们现在可以开始做有趣的事情了,比如在游戏中添加图像,把它们显示在屏幕上。但在我们这么做之前先让我们看看我们得在main.mxml文件中做些什么改变。
只在exitGame函数做了一个改变,该函数调用GameObjectManager的shutdown(关闭)函数。这将允许我们在离开游戏状态时GameObjectManager清除资源。现在让我们来看看在GameObjectManager中做的改变。
GameObject.as
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(GameObjects用来代表在游戏中的任何元素,将在下面说明)。GameObjectManager将在这个集合的所有元素上循环来升级或者绘制游戏对象GameObjects。另外两个集合:newGameObjects和removedGameObjects保存在绘制循环中生成或者删除的游戏对象GameObjects。这么做的原因是当你在某个集合的元素的值上面循环时改变这个集合是不对的做法,下面的代码是一个你想要避免的例子。
2010年07月10日
在第三部分我们将增加一些图像资源并把它们显示在屏幕上
标签: Flex, Flash, tutorial, Actionscript, game
转到: 第一部分
在写一个新程序的时候,总存在这么一个点,从这儿你可以看见你的劳动成果。通过实现状态和绘制的管道,我们现在可以开始做有趣的事情了,比如在游戏中添加图像,把它们显示在屏幕上。但在我们这么做之前先让我们看看我们得在main.mxml文件中做些什么改变。
只在exitGame函数做了一个改变,该函数调用GameObjectManager的shutdown(关闭)函数。这将允许我们在离开游戏状态时GameObjectManager清除资源。现在让我们来看看在GameObjectManager中做的改变。
GameObject.as
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(GameObjects用来代表在游戏中的任何元素,将在下面说明)。GameObjectManager将在这个集合的所有元素上循环来升级或者绘制游戏对象GameObjects。另外两个集合:newGameObjects和removedGameObjects保存在绘制循环中生成或者删除的游戏对象GameObjects。这么做的原因是当你在某个集合的元素的值上面循环时改变这个集合是不对的做法,下面的代码是一个你想要避免的例子。