Search the Community
Showing results for tags 'collision-detection'.
-
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. . I can't figure out what I'm doing wrong here.
-
- sprite
- hard-coding
-
(and 1 more)
Tagged with: