Jump to content

Getting an array of what layers are at a specific point


Lomaz
 Share

Recommended Posts

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

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.

Link to comment
Share on other sites

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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