Rishav Sharan
Rishav Sharan

Reputation: 2932

How to load simple tmx file in Haxeflixel?

I am trying to use a simple tmx map in my haxeflixel app using the flixel-addons library.

My tmx map has a single layer with all the tiles in it. there is nothing special about the map at all. I tried to use the TiledMap demo as reference and removed all the code which I thought I didnt need.

This is my customized map class;

class MapLoader extends TiledMap
{
    // Array of tilemaps used for collision
    public var backgroundTiles:FlxGroup;

    public function new(tiledLevel:Dynamic)
    {
        super(tiledLevel);

        backgroundTiles = new FlxGroup();

        FlxG.camera.setBounds(0, 0, fullWidth, fullHeight, true);

        // Load Tile Maps
        for (tileLayer in layers)
        {

            var processedPath = "assets/images/tiles/sheet.png";
            trace(processedPath);

            var tilemap:FlxTilemap = new FlxTilemap();
            tilemap.widthInTiles = width;
            tilemap.heightInTiles = height;
            tilemap.loadMap(tileLayer.tileArray, processedPath, 128, 64, 0, 1, 1, 1);

            backgroundTiles.add(tilemap);

        }
    }

}

and I am calling it in the PlayState like this;

    // Load the tilemap
    _map = new MapLoader(AssetPaths.map__tmx);

    // Load the tilesets
    add(_map.backgroundTiles);

The error I keep on getting is;

flixel.addons.editors.tiled.TiledMap has no field backgroundTiles

However, to me it seems that I am indeed adding this field the way it is done in the demo. What is it that I am doing wrong? My level of expertise in Haxe/Haxeflixel is beginner level.

To have a quick lookup at the code, please see https://github.com/rishavs/KingdomFail_Haxe/

The demo source that i am referring to is at https://github.com/HaxeFlixel/flixel-demos/tree/master/Editors/TiledEditor/source

Upvotes: 0

Views: 1651

Answers (2)

David Mekersa
David Mekersa

Reputation: 71

Actually, I use a simple way, using less add-on/external code, by simply export the map in Json, then using native Haxe Json parser.

var tmxTxt:String = File.getContent("assets/data/level-1.json");
var tmxData = Json.parse(tmxTxt);
trace(tmxData.layers[1].data);

Then I can access everything in the tmx.

FlxG.worldBounds.width = tmxData.width * tmxData.tilewidth;
FlxG.worldBounds.height = tmxData.height * tmxData.tileheight;

_collisionMap = new FlxTilemap();
_collisionMap.loadMapFromArray(tmxData.layers[1].data, tmxData.width, tmxData.height, "assets/images/mariou-tileset.png", TILE_WIDTH, TILE_HEIGHT, null, 1);
add(_collisionMap);

Using this way I'm independent with the Tiledmap addon, so I don't care if something new is added to the tmx format, I just have to change my code.

The current Tiled sample, provided on the Haxeflixel website, use a very odd method, requiring some private properties to get the tileset name... while everything is in the tmx format and easy to read with Json.

Upvotes: 0

Thorbjørn Lindeijer
Thorbjørn Lindeijer

Reputation: 2112

Instead of

private var _map:TiledMap;

try doing:

private var _map:MapLoader;

Otherwise the compiler will not realize the instance actually has the property you declared in the subclass.

Design-wise you may want to think about whether it really makes sense to define MapLoader as a subclass of the TiledMap. It seems to me that you only need some function to create the FlxGroup you're looking for.

Upvotes: 3

Related Questions