This is a quick tutorial on loading Ogmo2 levels in FlashPunk. As an added bonus, I’ll cover how simple it is to layer tile sets in your game world.
NOTE: I used Oryx’s free tile set for the tutorial.
Okay let’s get started! You’ll need to download the tutorial project zip as a starting point for this tutorial. The archive contains everything, but at this point you just want to turn your attention to the Ogmo project and level files found in the assets folder.
PRO TIP: As of this moment in time, there is a bug in Ogmo 2 where if you resize the window the tiles will disappear. In order to combat this, resize your editor window to the desired dimensions prior to loading a level. If you don’t heed this warning and you get invisible tiles, simply close the project and reopen it.
Above is the level file you will find in the archive. As you may have noticed, I have two tile layers – one for the bottom (or “floor”) tiles and the another for the top (or “detail”) tiles. This allows you to place other tiles on top of each other without the bottom one being over written. As you will see below, you just need to make second tile layer with the same properties to achieve this.
Okay, so how do you actually get the level into your project you ask? Well it’s quite simple really.
You’ll want to add a new class to your project that extends FlashPunk’s entity class:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Level extends Entity { private var xml:XML; private var tile_layer1:Tilemap; private var tile_layer2:Tilemap; private var grid:Grid; public function Level(rawData:Class) { super(0, 0); // to check for level collisions type = "level"; // FlashPunk function makes get all that data super easy! this.xml = FP.getXML(rawData); } } |
You’ll need a variable to hold the parsed XML data, one for each tileset, and one for the grid.
Many people who have seen Zach’s tutorial use a slightly more verbose method of getting the XML data. However, since that video a function in FP called FP.getXML() has been added that simplifies this process, which I’ve used my my constructor.
In this next block of code is the added() function of the entity, which will be loading up our tilesets:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
override public function added():void { super.added(); // load tiles // bottom tile layer tile_layer1 = new Tilemap(Assets.SPR_TILES, xml.@width, xml.@height, 16, 16); tile_layer1.loadFromString(xml.tile_layer1); trace(xml.tile_layer1); // top tile layer (details) tile_layer2 = new Tilemap(Assets.SPR_TILES, xml.@width, xml.@height, 16, 16); tile_layer2.loadFromString(xml.tile_layer2); // set graphic to graphiclist of tilesets, order matters! graphic = new Graphiclist(tile_layer1, tile_layer2); } |
Here we want to initialize the tilemaps with the width and height values from the XML data of the Ogmo level. Then use the loadFromString function available in the tilemap class which loads tiles from a string, which is exactly how Ogmo formats it. How handy! Then we just have to construct a graphic list and assign it to our graphic.
PRO TIP: I’d like to take the time to point out that you use the “@” when you want to access a value within an XML tag. For example:
|
1 2 3 4 5 |
<data> <company name="Awesome Co"> <employee id="1">John Doe</employee> </company> </data> |
To get the “name” value from the company node: “data.company.@name”. If you want to get value between XML tags, leave out the “@” and use the name of the root node. So to get the employee name: “data.company.employee”.
Next we’ll want to use the same principles to load the grid. Keep in mind this is still within the added() function of our Level class:
|
1 2 3 4 5 6 |
// load the collision grid grid = new Grid(xml.@width, xml.@height, 16, 16); grid.loadFromString(xml.grid, ""); // set the grid to the mask of this entity mask = grid; |
However, note that Ogmo doesn’t use the FP default comma (“,”) for the column delimiter. You’ll need to pass in double quotes for the delimiter parameter so it is loaded correctly.
Finally, we need to add our entities. Typically, you have more than just one instance of any given entity, so we’ll want to loop through each one and add it to the game. We can do this by creating an XMLList and assigning a parent node to it. Then all we need to do is loop through the list and add each entity:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// load entities var list:XMLList; // holds an xmllist var element:XML; // a specific xml element // for each player in the xml list list = xml.entities.player; for each (element in list) { // add it world.add(new Player(element.@x, element.@y)); } // for each coin in the xml list list = xml.entities.coin; for each (element in list) { // add it world.add(new Coin(element.@x, element.@y)); } |
You can also pass other XML values into the constructors of your entities if they have other properties. For example, let’s say our coin has a “pointWorth” value:
|
1 |
world.add(new Coin(element.@x, element.@y, element.@pointWorth)) |
Just make sure you define the property in your Ogmo project as well!
That concludes the tutorial! If you have any questions, please leave a comment, email me, or find me on the FlashPunk forums.