Jump to content

Retrieving tilemap tile by atlas frame name


Recommended Posts

Take the following code:


  preload: function() {    this.load.tilemap(this.level_key, 'assets/levels/test_01.json', null, Phaser.Tilemap.TILED_JSON);    this.load.atlasJSONHash(this.atlas_key, 'assets/images/tiles.png', 'assets/images/tiles.json');  },  // [...]  // These called from `create`:  createMap: function() {    // Add tilemap    this.map = this.game.add.tilemap(this.level_key);    // Attach tiles    this.tileset = this.map.addTilesetImage(this.tiled_tileset_key, this.atlas_key);    // Create layers    // [...]    this.charactersLayer = this.map.createLayer('Characters');  },  createPlayer: function() {    // Find player tile    var player_frame = this.game.cache.getFrameByName(this.atlas_key, this.texture_packer_keys.player);    var player_index = player_frame.index + 1; // atlas 0-based, but tilemap tileset is 1-based    var player_tile = this.map.searchTileIndex(player_index, 0, false, 'Characters');    // Create and center player sprite    this.player = this.game.add.sprite(player_tile.worldX, player_tile.worldY, this.atlas_key, this.texture_packer_keys.player);    // Remove placeholder tile    this.map.removeTile(player_tile.x, player_tile.y, 'Characters');    // [...]  },
Is there a simpler (built-in) way of retrieving a tile from a tilemap using its atlas frame name?
I've scoured the `this.map` Tilemap object, and can't find any reference to the original atlas names, since it seems that `this.map.addTilesetImage(...)` just carries over indexes?
Additional question: would I avoid this issue by using an Object layer for my player placeholder?


Link to comment
Share on other sites

  • 1 year later...
  • 10 months later...

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...