Advertisement
Guest User

TiledMap

a guest
Aug 11th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 2.71 KB | None | 0 0
  1. package;
  2. import flixel.FlxState;
  3. import flixel.FlxG;
  4. import haxe.io.Path;
  5. import flixel.tile.FlxTilemap;
  6. import flixel.addons.editors.tiled.TiledMap;
  7. class MenuState extends FlxState
  8. {
  9. override public function create():Void
  10. {
  11. super.create();
  12. // Load the TMX data
  13. var tiledLevel:TiledMap = new TiledMap("assets/data/Main.tmx");
  14. // Get map variables
  15. var tileSize = tiledLevel.tileWidth;
  16. var mapW = tiledLevel.width;
  17. var mapH = tiledLevel.height;
  18. // Loop through each tile layer and render tile map
  19. for (layer in tiledLevel.layers)
  20. {
  21. var layerData:Array<Int> = layer.tileArray;
  22. var tilesheetName:String = layer.properties.get("tilesheet");
  23. var tilesheetPath:String = "assets/images/" + tilesheetName;
  24. // Finally, create the FlxTilemap and get ready to render the map.
  25. var level:FlxTilemap = new FlxTilemap();
  26. // If we're passing an array of data, the level needs to know
  27. // how many columns of data to read before it moves to a new row,
  28. // as noted in the API page:
  29. // http://api.haxeflixel.com/flixel/tile/FlxTilemap.html#loadMap
  30. level.widthInTiles = mapW;
  31. level.heightInTiles = mapH;
  32. // Note: The tilesheet indices are continuous! This means,
  33. // if there is more than one tilesheet, the 2nd tilesheet's
  34. // starting index right after the 1st tilesheet's last index.
  35. // e.g.
  36. // - tilesheet 1 has 100 tiles (index = 1-100)
  37. // - tilesheet 2 has 100 tiles (index = 101-200 instead of 1-100)
  38. //
  39. // Note2: that the gid "0" is reserved for empty tiles
  40. var tileGID:Int = getStartGid(tiledLevel, tilesheetName);
  41. // Render the map.
  42. // Note: the StartingIndex is based on the tilesheet's
  43. // startingGID rather than the default 1.
  44. level.loadMap(layer.tileArray, tilesheetPath, tileSize, tileSize, FlxTilemap.OFF, tileGID);
  45. add(level);
  46.     }
  47. }
  48.     function getStartGid (tiledLevel:TiledMap, tilesheetName:String):Int
  49.     {
  50.         // This function gets the starting GID of a tilesheet
  51.         // Note: "0" is empty tile, so default to a non-empty "1" value.
  52.         var tileGID:Int = 1;
  53.     for (tileset in tiledLevel.tilesets)
  54.         {
  55.                 // We need to search the tileset's firstGID -- to do that,
  56.                 var tilesheetPath:Path = new Path(tileset.imageSource);
  57.                 // we compare the tilesheet paths. If it matches, we
  58.                 // extract the firstGID value.
  59.                 var thisTilesheetName = tilesheetPath.file + "." + tilesheetPath.ext;
  60.             if (thisTilesheetName == tilesheetName)
  61.             {
  62.                 tileGID = tileset.firstGID;
  63.             }
  64.         }
  65.         return tileGID;
  66.     }
  67.     override public function update():Void
  68.     {
  69.         super.update();
  70.     }
  71.     override public function destroy():Void
  72.     {
  73.         super.destroy();
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement