Jump to content

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


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 {

function moveDown(map, layers) {

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


Here is information on the hasTile() function.


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


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.

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.


  • Recently Browsing   0 members

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