Lomaz Posted January 15, 2016 Share Posted January 15, 2016 Hello! so how my game works is simple: depending on what layers a sprite is standing on, different callbacks need to be done. Right now I'm doing it like this: layers.forEach(function(entry) { if((map.getTileWorldXY(player.body.x, player.body.y,48,48, entry)) != null){ where layers is an array generated like this: map.layers.forEach(function(entry) { layers.push(map.createLayer(entry.name)); }); This works well enough for me right now, but my maps somtimes have 15+ different layers on them, so i can imagine this foreach loop to be fairly stressful to execute every time on update. I consitered having the check only be preformed once every X frames, that's better but still not ideal. So what I'm asking is, instead of looking at each layer and seeing if the player is standing on it, can i look at the player and see what layers it IS interacting with so i can make function calls approperately? If you have any other questions, please ask! Link to comment Share on other sites More sharing options...
WombatTurkey Posted January 15, 2016 Share Posted January 15, 2016 Get the distance the player is relative to the tile. You need to somehow create a list of x, y values in Tiled (can use the objects layer) and then add them into an array and use that to check the distance between you and your player in Phaser's update function. getRange = function(a, b{ var c = 0, d = 0, c = b.x - a.x, d = b.y - a.y; return Math.sqrt(c * c + d * d) } Player = { x: 10, y: 10 } TiledSprite = { x: 10, y: 5 } Then, in phaser's update method if(getRange(Player, TiledSprite) < 5){ console.log('Player is overlapping this tile'); } You can ofcourse have multiple objects just push them into an array and iterate them in the update function. hoskope 1 Link to comment Share on other sites More sharing options...
drhayes Posted January 15, 2016 Share Posted January 15, 2016 Are you only doing these checks for the player? 15 layers is your max? Don't worry about the performance implications yet. If you are making these checks for every sprite I'd start measuring to see if there is a problem before trying to solve the problem. It'll save you lots of work in the long run. Link to comment Share on other sites More sharing options...
Recommended Posts