mondord Posted May 5, 2014 Share Posted May 5, 2014 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,0,0,0,0,40,1,1,1,1,10,0,2,0,0,01,1,1,1,1,03,0,0,0,0,00 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. eg. 0,2,0,0,0,10,1,1,1,2,10,1,3,1,0,10,1,0,1,0,14,1,0,2,0,1 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! Cheers,Chris Link to comment Share on other sites More sharing options...
mondord Posted May 6, 2014 Author Share Posted May 6, 2014 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? jackrugile, Nebulocity, Heppell08 and 1 other 4 Link to comment Share on other sites More sharing options...
jackrugile Posted January 4, 2015 Share Posted January 4, 2015 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 More sharing options...
Recommended Posts