Jump to content

Position sprites and player based on tile map


Recommended Posts

Hi all,


Just starting out with Phaser (such an amazing tool!) and playing around with a simple game using tileset maps.


What I'd like to do is position the player and some enemies based on tile index in the map.  I'm currently using CSV map data for simplicity, but could be JSON too I guess. I'm using Phaser version 2.0.4.


eg for the following map:



0 is empty space, 1 is a wall (impassable), 2 is a bad guy and 3 is the player start position.  4 would be the exit to the next level.


This will let me generate many levels very simply and let me have different player and enemy start positions all over the place without altering the game code.






would work the same way and generate a whole new level with the player starting in the middle instead of the bottom, etc.etc.etc.


I've got the tileset loading in, the proper images are rendering in the world, collision and movement through the maze is all good. Currently I'm placing the player and enemies with hardcoded pixel values.


What I'm missing is, I can't seem to find a function that lets me get the x/y pixel position of a tile by its index type so that I can find the start position on the world using the tilemap instead of hard coding it.


So I guess I want a function that I can pass the index of 3, and get the x/y pixel coordinates from that Tile as its found in the map. I can then use those x/y coords to place the player sprite in the right position.


Does such a thing exist, or is there an alternate way I could achieve this by other means?


I guess the ideal would be a function that returns an array of all tiles in the map that match the index, which i could loop through and call a function on each tile using its location data. That way I could use the same function for the player start, and any number of enemies.   I read on another post that I could use the Tilemap.createFromObjects method to do the enemies.  I think the player is a different case though?


Many thanks for any help or pointers!





Link to comment
Share on other sites

Well, I muddled through and came up with a solution that seems to work - may not be the most efficient however:

map.forEach(function(tile) {          if(tile.index === 3) {        player.x = tile.worldX+16;        player.y = tile.worldY+16;      }    }, 0,0,0,20,20);

So this loops through all the tiles in the map, and checks for the one with index of 3 (that represents the player's starting tile). It then positions the player onto the world coords of the tile, with an offset of 16  because I'm using 32x32 tiles to get the position in the centre of the tile onscreen.

I can repeat this routine for the enemies as well, so that the enemy tile positions get sprites on top of them.

Anyone got a better solution they can offer?

Link to comment
Share on other sites

  • 7 months later...

I don't have a better solution, but I just wanted to say thank you for posting yours. Been looking for an hour on how to deal with a single object from a tilemap, but apparently no one else has made a game with a single start/finish/hero position, only groups of objects. Frustrating, but your method works great!

Link to comment
Share on other sites


  • Recently Browsing   0 members

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