Jump to content

My hard-coded collision detection function works only for right and down directions


evilwizard
 Share

Recommended Posts

I have a tilemap with multiple layers that should be impassable. I tried multiple times with setCollision() and setCollisionBetween() but could never get it to work, so I decided to do a bit of hard-coding. I have a 32 x 32 tile-map and this function works for 2 out of 4 directions. The directions this works for are right and down

I can solve this easily if I can just figure out the problem with one direction. I simplified this code as much as possible. The moveUp() function is the one that doesn't work, whereas the moveDown() function is the one that's working.

function moveUp(map, layers) {

    if (layers.every(layer => !map.hasTile(x, y - 1, layer))) {
        hero.body.velocity.setTo(0, -150);
    }
    else {
        stopMovement();
    }

}
    
function moveDown(map, layers) {

    if (layers.every(layer => !map.hasTile(x, y + 1, layer))) {
        hero.body.velocity.setTo(0, 150);
    }
    else {
        stopMovement();
    }

}

Here is information on the hasTile() function.

https://phaser.io/docs/2.6.2/Phaser.Tilemap.html#hasTile

  • x: X Position to check if a tile exists
  • y: Y Position to check if a tile exists
  • Layer: The layer to set

I'm checking whether the map has a tile in the collision layers at the player's X Position on the tile map and the Y Position is the tile directly above or below the player. If there is no collision tile, the player is free to walk.

When a player moves up, there is an entire tile that a player cannot access (32px), whereas, when a player moves down, there is no space. To demonstrate this, take a look at the attachments. The problem probably lies with the coordinates of the collision sprite.

.directionUp.png.f9fa3f54d39a0758d7705dc125e680a1.pngdirectiondown.png.98c32e1c17e1237932a9bec10293dd57.png

I can't figure out what I'm doing wrong here.

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...