FLEX和Actionscript开发FLASH游戏 10-4
2010年12月09日
使用FLEX和Actionscript开发FLASH 游戏-平铺式背景渲染
Level.as
package
{
import flash.events.*;
import flash.geom.*;
import flash.media.*;
import flash.net.*;
import flash.utils.*;
import mx.core.*;
public class Level
{
protected static var instance:Level=null;
protected static const TimeBetweenLevelElements:Number=2;
protected static const TimeBetweenClouds:Number=2.5;
protected static const TimeToLevelEnd:Number=2;
protected var nextDefinition:Array=null;
protected var levelID:int=0;
protected var totalTime:Number=0;
protected var timeToNextCloud:Number=0;
protected var timeToLevelEnd:Numbder=0;
protected var backgroundMusic:SoundChannel=null;
public var levelEnd:Boolean=false;
static public function get Instance():Level
{
if(instance==null)
instance=new Level();
return instance;
}
public function Levle()
{
}
public function startup(levelID:int):void
{
new Player().startupPlayer();
timeToLevelEnd=TimeToLevelEnd;
levelEnd=false;
backgoundMusic=ResourceManager.Track1FX.play(0,int .MAX_VALUE);
this.totalTime=0;
this.levelID=levelID;
nextDefinitions=LevelDefinitions.Instance.getNextL evelDefinitionElements(levelID,0);
var tileDefinition:TiledBackgroundDefinition=LevelDefi nitions.Instance.levelTileMaps[levelID] as
TiledBackgroundDefinition;
if(tileDefinition!=null)
(TileBackground.pool.ItemFromPool as TiledBackground).startupTiledBackground(tileDefini tion);
}
public function shutdown():void
{
backgroundMusic.stop();
backgroundMusic=null;
}
public function enterFrame(dt:Number):void
{
totalTime+=dt;
if(nextDefinitions==null)
{
if(Enemy.pool.NumberOfActiveObjects==0)
levelEnd=true;
}
else { var nextLevelDefTime:Number=(nextDefinitions[0] as LevelDefinitionElement).time; if(totalTime>=nextLevelDefTime) { for each(var levelDefElement:LevelDefinitionElement in nextDefinitions) levelDefElement.func(); nextDefinitions=LevelDefinitions.Instance.getNextL evelDefinitionElements(levelID,nextLevelDefTime); } } //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), ZOrders.CLOUDSBELOWAORDER, 75); } if(levelEnd) { timeToLevelEnd-=dt; var scale:Number=timeToLevelEnd/TimeToLevelEnd; if(scale<0)scale=0; var transform:SoundTransform=backgroundMusic.soundTran sform; transform.volume=scale; backgroundMusic.soundTransform=transform; } if(timeToLevelEnd<=0) Application.application.currentState="LevelEnd"; } } } 正如你能见到的我们删除了所有原来用来随机生成BackgroundLevelElement的代码,增加了三行代码来生成新的TiledBackground类。
通过增加了绘制平铺式背景的功能,我们得到了一种方法来生成一个好看的级别,且不用花太多存储空间。再次感谢有免费的可用的地图编辑工具和背景覆盖块集使得生成这些级别变得相当容易,就即便是你没有很多艺术能力。
在http://flexfighters.sourceforge.net/flexfighters10 .html处可查看最终效果,从https://sourceforge.net/project/showfiles.php?grou p_id=241490&package_id=293860&release_id=635625处可下载资源
2010年12月09日
使用FLEX和Actionscript开发FLASH 游戏-平铺式背景渲染
Level.as
package
{
import flash.events.*;
import flash.geom.*;
import flash.media.*;
import flash.net.*;
import flash.utils.*;
import mx.core.*;
public class Level
{
protected static var instance:Level=null;
protected static const TimeBetweenLevelElements:Number=2;
protected static const TimeBetweenClouds:Number=2.5;
protected static const TimeToLevelEnd:Number=2;
protected var nextDefinition:Array=null;
protected var levelID:int=0;
protected var totalTime:Number=0;
protected var timeToNextCloud:Number=0;
protected var timeToLevelEnd:Numbder=0;
protected var backgroundMusic:SoundChannel=null;
public var levelEnd:Boolean=false;
static public function get Instance():Level
{
if(instance==null)
instance=new Level();
return instance;
}
public function Levle()
{
}
public function startup(levelID:int):void
{
new Player().startupPlayer();
timeToLevelEnd=TimeToLevelEnd;
levelEnd=false;
backgoundMusic=ResourceManager.Track1FX.play(0,int .MAX_VALUE);
this.totalTime=0;
this.levelID=levelID;
nextDefinitions=LevelDefinitions.Instance.getNextL evelDefinitionElements(levelID,0);
var tileDefinition:TiledBackgroundDefinition=LevelDefi nitions.Instance.levelTileMaps[levelID] as
TiledBackgroundDefinition;
if(tileDefinition!=null)
(TileBackground.pool.ItemFromPool as TiledBackground).startupTiledBackground(tileDefini tion);
}
public function shutdown():void
{
backgroundMusic.stop();
backgroundMusic=null;
}
public function enterFrame(dt:Number):void
{
totalTime+=dt;
if(nextDefinitions==null)
{
if(Enemy.pool.NumberOfActiveObjects==0)
levelEnd=true;
}
else { var nextLevelDefTime:Number=(nextDefinitions[0] as LevelDefinitionElement).time; if(totalTime>=nextLevelDefTime) { for each(var levelDefElement:LevelDefinitionElement in nextDefinitions) levelDefElement.func(); nextDefinitions=LevelDefinitions.Instance.getNextL evelDefinitionElements(levelID,nextLevelDefTime); } } //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), ZOrders.CLOUDSBELOWAORDER, 75); } if(levelEnd) { timeToLevelEnd-=dt; var scale:Number=timeToLevelEnd/TimeToLevelEnd; if(scale<0)scale=0; var transform:SoundTransform=backgroundMusic.soundTran sform; transform.volume=scale; backgroundMusic.soundTransform=transform; } if(timeToLevelEnd<=0) Application.application.currentState="LevelEnd"; } } } 正如你能见到的我们删除了所有原来用来随机生成BackgroundLevelElement的代码,增加了三行代码来生成新的TiledBackground类。
通过增加了绘制平铺式背景的功能,我们得到了一种方法来生成一个好看的级别,且不用花太多存储空间。再次感谢有免费的可用的地图编辑工具和背景覆盖块集使得生成这些级别变得相当容易,就即便是你没有很多艺术能力。
在http://flexfighters.sourceforge.net/flexfighters10 .html处可查看最终效果,从https://sourceforge.net/project/showfiles.php?grou p_id=241490&package_id=293860&release_id=635625处可下载资源