Contact Me or check out the Producerism Blog

FlashPunk, DAME and Lua Tutorial (Part 3)

01/27/2011


Since I've already covered some of the various game frameworks for Flash, decided on a project to make, and explained all of the components within the project - I think it's finally time to get into some code.  If you haven't already downloaded the project files, grab them here.

First, take a look at the XML structure we'll be using:

<level>
	<tiles layer="template4" x="1440" y="0" width="480" height="480" tileWidth="32" tileHeight="32" widthInTiles="15" heightInTiles="15" >
		<tile tx="96" ty="0" x="0" y="0"/>
	</tiles>
	<sprites layer="template4">
		<sprite className="Balloon" x="1484" y="349" width="35" height="45" points="1" />
		<sprite className="Balloon" x="1621" y="2" width="35" height="45" points="1" spawnShape="74A916C5-C96B-62BE-6540-024B6D1237CB" spawnAmount="2" color="0x00ff00" />
		<sprite className="Balloon" x="1818" y="73" width="35" height="45" points="1" />
		<sprite className="Balloon" x="1750" y="343" width="35" height="45" points="1" spawnShape="74A916C5-C96B-62BE-6540-024B6D1237CB" spawnAmount="5" />
		<sprite className="FlyingPig" x="1843" y="1" width="60" height="51" spawnShape="5E3E76E3-431E-E1E1-B26C-930B32DB8AB1" spawnAmount="5" />
		<sprite className="Balloon" x="1629" y="346" width="35" height="45" points="1" />
	</sprites>
	<shapes layer="template1">
		<shape type="rectangle" x="1663" y="286" width="191" height="159" guid="74A916C5-C96B-62BE-6540-024B6D1237CB" />
		<shape type="rectangle" x="1659" y="64" width="257" height="160" guid="5E3E76E3-431E-E1E1-B26C-930B32DB8AB1" />
	</shapes>
</level>

It looks a bit complicated, but it's really not at all.  First of all, notice that I've only included a single line for each tag.  In reality, the entire file is over 1000 lines long (don't worry, Lua does all of that for us!).   On line 1 you can see that the root node for the entire XML file is called level.  This could be called anything, usually it's just <xml> but in this case I went with something that made sense to me.

The main tags

Within that root level tag, there are only 3 other main tags:

<tiles>

<tiles layer="template4" x="1440" y="0" width="480" height="480" tileWidth="32" tileHeight="32" widthInTiles="15" heightInTiles="15" >

This contains all of the tilemap information for the level.  Each time within DAME that you click on the canvas and paint a tile (or use the tile matrix tool), it will be exported into the xml file within this tag.  More on that in a second.  Note the attributes within this tag:

<sprites>

<sprites layer="template4">

Not surprisingly, this tag contains all of the sprites within the level.  As mentioned above, the <tiles> tag actually has all of the offset information needed to position the sprites.  The <sprites> tag only has one attribute actually: layer, which is set to "template4."

<shapes>

<shapes layer="template4">

Just like the <sprites> tag, shapes gets it's offset information from the <tiles> tag with the same layer name, "template4."

The Child Tags

We're almost done covering the xml format!  Within each of the main parent tags, there are child tags.  For <tiles> there is <tile>, for <sprites> there is <sprite> and for <shapes> there is <shape>.  Let's go over each one, since this is where all the important stuff is:

<tile>

<tile tx="96" ty="0" x="0" y="0"/>

One of these tags is created each time you paint a tile within DAME.  There are only 4 attributes, which are all very straight forward:

<sprite>

<sprite className="FlyingPig" x="1843" y="1" width="60" height="51" spawnShape="5E3E76E3-431E-E1E1-B26C-930B32DB8AB1" spawnAmount="5" />

For the sprite example, let's look at this flying pig.  Out of all the different elements within the XML file, the <tiles> (plural) and <sprite> (non-plural) tags will usually have the most attributes (at least the way I've coded it).

<shape>

<shape type="rectangle" x="1659" y="64" width="257" height="160" guid="5E3E76E3-431E-E1E1-B26C-930B32DB8AB1" />

Finally, this is the last element to look at.  While a circle is used for example in this project, there is no code to support using circles as spawnShapes.  So let's take a look at a rectangle, which is being used as a spawnShape.

For a visual example of whats going on here, let's look at this template within DAME:

The FlyingPig <sprite> in this example, is the one you see in the top right of the template.  The <shape> being used as a spawnShape for the Flying Pig, is the rectangle right below him (or her).  You can also see by the marquee, that the FlyingPig sprite is selected within the DAME editor, and there are two custom properties (on the bottom left).  This is where spawnShape and spawnAmount are defined.

Conclusion of Part 3

Phew, I wasn't sure how much detail I wanted to go into with this XML file, but turns out, I wanted quite a bit.  You should now understand how to create a level within DAME editor, and what that information will look like when it comes out of DAME.  The next step of this tutorial will be to explain exactly how all of this information is converted, using Lua (which is not an acronym).

Continue to Part 4


Leave a Reply