Flash Game Development with Flex and Actionscript - Tiled Background Rendering (Page 4 of 4)

Article by Matthew Casperson (4,880 pts ) , published Nov 6, 2009

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 nextDefinitions:Array = null;

protected var levelID:int = 0;

protected var totalTime:Number = 0;

protected var timeToNextCloud:Number = 0;

protected var timeToLevelEnd:Number = 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 Level()

{

}

public function startup(levelID:int):void

{

new Player().startupPlayer();

timeToLevelEnd = TimeToLevelEnd;

levelEnd = false;

backgroundMusic = ResourceManager.Track1FX.play(0, int.MAX_VALUE);

this.totalTime = 0;

this.levelID = levelID;

nextDefinitions = LevelDefinitions.Instance.getNextLevelDefinitionElements(levelID, 0);

var tileDefinition:TiledBackgroundDefinition = LevelDefinitions.Instance.levelTileMaps[levelID] as TiledBackgroundDefinition;

if (tileDefinition != null)

(TiledBackground.pool.ItemFromPool as TiledBackground).startupTiledBackground(tileDefinition);

}

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.getNextLevelDefinitionElements(levelID, nextLevelDefTime);

}

}

// add cloud

timeToNextCloud -= dt;

if (timeToNextCloud <= dt)

{

timeToNextCloud = TimeBetweenClouds;

var cloudBackgroundLevelElement:BackgroundLevelElement = BackgroundLevelElement.pool.ItemFromPool as BackgroundLevelElement;

cloudBackgroundLevelElement.startupBackgroundLevelElement(

ResourceManager.CloudGraphics,

new Point(Math.random() * Application.application.width, -ResourceManager.CloudGraphics.bitmap.height),

ZOrders.CLOUDSBELOWZORDER,

75);

}

if (levelEnd)

{

timeToLevelEnd -= dt;

var scale:Number = timeToLevelEnd / TimeToLevelEnd;

if (scale < 0) scale = 0;

var transform:SoundTransform = backgroundMusic.soundTransform;

transform.volume = scale;

backgroundMusic.soundTransform = transform;

}

if (timeToLevelEnd <= 0)

Application.application.currentState = "LevelEnd";

}

}

}

As you can see we have stripped out all the old code that was responsible for randomly creating the BackgroundLevelElement and have added 3 lines of code to now create a new TiledBackground class.

By adding the ability to render a tiled background we have a way of creating nice looking levels that don't take up too much memory. And thanks to some freely availble map editing tools and background tile sets it is quite easy to generate these levels even if you don't have a lot of artistic ability.

Go back to Flash Game Development with Flex and ActionScript

Related Files

Images

The game in action
Showing page 4 of 4

14 Comments

Showing page 1 of 2 (14 Comments)
Nov 20, 2009 9:02 PM
Aryadi
RE: Flash Game Development with Flex and Actionscript - Tiled Background Rendering
Well, I've figured out the problem now
thanks for the great tutorial :D
Nov 18, 2009 4:41 PM
RE: Flash Game Development with Flex and Actionscript - Tiled Background Rendering
The C# tool to convert the XML to AS3 was a quick hack. ActionScript has native support for XML, so you could read the TaT level file directly.
Nov 18, 2009 1:32 PM
kentpachi
Thanks
Thank you very much a great tutorial to start with !
Nov 18, 2009 1:32 PM
Aryadi
RE: Flash Game Development with Flex and Actionscript - Tiled Background Rendering
Hi Matthew, thanks I've downloaded the resource parser

there's one problem though, the resource parser doesn't seem to parse the flip property of the tiles. So in TaT level editor the tiles are flipped vertically (90 degrees) but when I used the parser to parse the xml to as3, I noticed that there are no flip property on the right hand side textbox

any suggestion?
Nov 15, 2009 4:59 PM
RE: Flash Game Development with Flex and Actionscript - Tiled Background Rendering
The Tat Resource Parser executable can be downloaded from http://www.brighthub.com/hubfolio/matthew-casperson/media/p/56016.aspx
Nov 15, 2009 1:36 PM
Aryadi
Tat parser
Hi, i just created a level with TaT and now I want to convert it into an appropriate array value. So I downloaded your app from the svn repo but it's just a solution file with a directory contains the source code, without the application.

I think if I just compile it then I could get the application. The problem is I don't have visual studio, so how do you compile a VS project without a VS?

Oh yeah, please don't tell me to download the VS first because I did. And it didn't work (it has some sort of a 'suite integration error' kind of error) and I've been asking people about this suite integration toolkit but none has bring me an answer to deal with the issue
Nov 5, 2009 3:24 PM
RE: Flash Game Development with Flex and Actionscript - Tiled Background Rendering
Yes, you are free to use all the sprites in your own projects.
Nov 5, 2009 11:43 AM
Aryadi
Out of curiosity
Oh, I know now that I need to use the TaT to make my own level so I won't be asking that

just curious I wanna ask can I use the tiled sprites used for background in my own project? I know the ships and bullets are GPL but what about the tiled background?
Oct 31, 2009 5:00 AM
Aryadi
the height
um... I'm not really good at multidimensional arrays so I'll ask again. if I want to modify the level, say, change the height to around 70 then which line should I change?

I've counted that line 91 is 15 elements in a single [ ] bracket and there are 49 lines from line 91 to 139 while in your default code says the width is 40 tiles and the height is 40 tiles. So... which should I modify?
Oct 30, 2009 5:38 PM
RE: Flash Game Development with Flex and Actionscript - Tiled Background Rendering
The tiles property is a multidimensional array that will contain references to the GraphicsResources that will be used to draw the background. When populated the tiles array will contain a dimension for the layers, then the rows and then finally the columns. For example tiles[1][4][5] would point to the GraphicsResource for the sixth column (tiles[1][4][5]) of the fifth row (tiles[1][4][5]) of the second layer (tiles[1][4][5]).

The null values indicate that the layer should not display any tile. This is used a lot for layers above the first ground level layer.

Showing page 1 of 2 (14 Comments)
 
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Browse Web Development