I have made my First HaxeFlixel Contribution
Recently, I have been working on my June One Game. I decided to take the level that my son built and turn it into a quick game. So I did what I thought was all that was needed and pulled it into a HaxeFlixel project. And it borked. This is what I got.
Compare that to the level my son created.
I hope you can see the problem. So I turned to the HaxeFlixel forums to figure out what went wrong.
There actually turned out to be 3 problems with my set up. The first was just the fact that nothing was showing up beyond the first layer. I could not figure it out myself. I was doing things exactly as I saw them in the two demos I based this off of. So what was the issue?
The problem turned out to be in the demos themselves. In the TiledLevel.hx file there is a call that loads the layer. This is how it looked in the demos:
tilemap.loadMap(tileLayer.tileArray, processedPath, tileSet.tileWidth, tileSet.tileHeight, 0, 1, 1, 1);
In this code, the first ‘1’ is the guid of the tileset that particular layer uses. Since the demos hardcoded a ‘1’ there, it would only look at the first tileset and then flip out because that is not what the game was looking for. So the change was suggested to make that line read as follows:
tilemap.loadMap(tileLayer.tileArray, processedPath, tileSet.tileWidth, tileSet.tileHeight, 0, tileSet.firstGID, 1, 1);
Now, the guid is read from the .tmx file rather than being hardcoded.
So now the layers were loading into the game, but they were coming in wrong.
So now the layers are loading but everything is off. In the level my son made, the trees and mountains were overlapped. This gave a nice look to the game.
Sadly, this was nothing that I could fix in HaxeFlixel. I had to completely redraw my tileset for the trees and mountains.
So once I redrew those and remade the level with them, it looked pretty much how my son envisioned it. But I had one last problem. Only the first collidible layer would register. Everything else had the player walking all over it.
This one I figured out on my own. In the function that checks for collision there is this loop:
for (map in collidableTileLayers) { // IMPORTANT: Always collide the map with objects, not the other way around. // This prevents odd collision errors (collision separation code off by 1 px). return FlxG.overlap(map, obj, notifyCallback, processCallback != null ? processCallback : FlxObject.separate); }
This turned out to be a very easy fix. If you look, you can see that regardless of if there is a collision or not, it always returned the result after the first layer. This means that the function was exited and no other layers were checked. So I fixed it to read as follows:
for (map in collidableTileLayers) { // IMPORTANT: Always collide the map with objects, not the other way around. // This prevents odd collision errors (collision separation code off by 1 px). if(FlxG.overlap(map, obj, notifyCallback, processCallback != null ? processCallback : FlxObject.separate)) { return true; } }
In this, it only returns true if there is an actual collision. That if statement helped out.
After getting a suggestion from someone on the forums, I decided to do a git pull request and have these changes added to the two demos I worked off of. This was my first time doing something like this. So it was completely new to me.
But the change went off without a hitch and it was merged into the dev branch of the HaxeFlixel Flixel-Demos repository.
So now, any new developers who look at those demos for help implementing Tiled Map Editor in their HaxeFlixel games will be able to avoid 2 of these 3 issues thanks to this effort.
I am hoping to be able to contribute far more in the future. I really want to do what I can to support this great technology and make it the best it can be. So look for more of this from me in the future.