Actionscript Tutorial: Add a Scrolling Tiled Background

Written by:  • Edited by: Linda Richter
Updated Nov 6, 2009

Along with discussing the advantages to using tiled backgrounds, this tutorial will show you how to add them.

In part 9 of the series we added the ability to define a level structure using a series of timed function calls. This works well for placing enemies on the screen, but is not so useful for drawing the background level. In this article we will add the ability to render a predefined tiled background.

Tiled backgrounds are made up of a handful of smaller repeated tiles arranged, in this case, in a grid. This approach has a number of advantages, but the biggest is that it reduces the memory requirements of the game. Using one single prerendered background image could conceivably take up several megabytes of memory per level. Of course using a prerendered background would give you the highest level of detail, but all the detail in the world doesn’t matter if the player clicks off the page because they are sick of waiting for the game to load. By contrast a tiled background will take up a fraction of the memory used by a prerendered background, and a good artist can still make some nice looking tiled backgrounds.

The first step in making a tiled background are the tiles themselves. I found a nice set of free tiled wilderness graphics from http://lostgarden.com/labels/free%20game%20graphics.html. The site also has another of other tiled resources sets which you may find interesting.

The next step is finding a level editor that will allows us to draw the levels through a graphical interface. Of course you could write your own (that’s another article series in itself), but luckily someone has already done the hard work for us. The TaT tile map editor from http://kotisivu.dnainternet.net/ttilli/tilemapeditor/download.htm will do the job nicely. It has a few nice features like layers and XML exporting that we will make use of.

And of course we need to add some code to render the tiled background within the game. First we need some way to hold the tiles background data. The TiledBackgroundDefinition class takes care of that for us. Lets look at the Actionscript code for that class now.

Faster Game Loading

package

{

public class TiledBackgroundDefinition

{

public var tiles:Array = null;

public var tileScrollRate:Number = 0;

public var tileWidth:int = 0;

public var tileHeight:int = 0;

}

}

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]) – remember that arrays have zero based indexers. The tileWidth and tileHeight properties define the size in pixels of the tiles that make up the level. And tileScrollRate defines the speed at which the tiled level scrolls underneath the player to give the illusion of movement.

Now that we have a way to define a tiled background we need a place to store the definitions. The LevelDefinitions class will be used to store these definitions. Lets take a look at the new Actionscript code for the LevelDefinitions class now.

Showing page 1 of 4

Comments

Showing all 18 comments
 
MMatz Jul 13, 2011 11:15 AM
BaseObject class
An explanation of the role of the class BaseObject in tutorial #10 would be nice...
Kapil May 27, 2010 10:08 AM
Excellent
i am really very excited after reading all the matter.
i thank u for giving such great material.
RCDMK Mar 27, 2010 10:52 PM
Nice Tutorial.
A very nice tutorial. Congratulations.

The only thing I don't realy like was the way you handled the tiled map. Thankfully, I've writed something similar to this in XNA (C#) and can be used here.

XML is the way I do it.

One more time, congratulations. That was just my way.
Sorry for my bad English.
doomie Jan 12, 2010 3:52 AM
counting
I got a little bit confused.... i want to count how many enemies were killed but i got confused and i don't know where to count, in what function....
pls help
Aryadi Nov 20, 2009 9:02 PM
RE: Actionscript Tutorial: Add a Scrolling Tiled Background
Well, I've figured out the problem now
thanks for the great tutorial :D
Matthew Casperson Nov 18, 2009 4:41 PM
RE: Actionscript Tutorial: Add a Scrolling Tiled Background
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.
kentpachi Nov 18, 2009 1:32 PM
Thanks
Thank you very much a great tutorial to start with !
Aryadi Nov 18, 2009 1:32 PM
RE: Actionscript Tutorial: Add a Scrolling Tiled Background
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?
Matthew Casperson Nov 15, 2009 4:59 PM
RE: Actionscript Tutorial: Add a Scrolling Tiled Background
The Tat Resource Parser executable can be downloaded from http://www.brighthub.com/hubfolio/matthew-casperson/media/p/56016.aspx
Aryadi Nov 15, 2009 1:36 PM
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
Matthew Casperson Nov 5, 2009 3:24 PM
RE: Actionscript Tutorial: Add a Scrolling Tiled Background
Yes, you are free to use all the sprites in your own projects.
Aryadi Nov 5, 2009 11:43 AM
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?
Aryadi Oct 31, 2009 5:00 AM
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?
Matthew Casperson Oct 30, 2009 5:38 PM
RE: Actionscript Tutorial: Add a Scrolling Tiled Background
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.

Aryadi Oct 30, 2009 2:49 PM
Background Level
Hi, it's a great tutorial, really great for people just starting flex

I have a question though, in Level description I notice we have a huge chunk of... what? 2 dimensional array? and GreenGraphicsID1 is being repeated over and over it filled one dimension of the array. What's it for? can you explain it?

second, about the second dimension of the array., if I have to guess I'd say it's where the levels are being defined, but I don't understand what are those nulls doing there?
abel Oct 13, 2009 3:16 AM
very nice
very thanks for a demo article example!but i have question that flash web game developing mode like this also?
NickM Aug 22, 2009 6:55 AM
RE: Actionscript Tutorial: Add a Scrolling Tiled Background
Many thanks for a great informative article.

NickM
Caryn Jun 24, 2009 10:26 AM
RE: Actionscript Tutorial: Add a Scrolling Tiled Background
Hi,

I'm having trouble extending your code for buttons inside the game. For example, I want to put a button at the bottom right of the game screen that goes back to the main menu. I would also like to be able to make the button appear and disappear.

I know that I could tell it to check whether the click was inside the button, but I would rather have an actionlistener on the button itself. Any direction would be appreciated.

Thanks
 
blog comments powered by Disqus
Email to a friend