Search code examples
haxetmxtiledhaxeflixel

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


Solution

  • 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.