Kejender
Kejender

Reputation: 53

Player collision is not working in Phaser3 game

I have gone through a few tutorials and short courses on Phaser3 and I started working on my own game. The game mechanics in the example games are different from what I'm trying to do, so I tried to alter them accordingly. This lead in some strange results.

My starting point was a platform jumper game. It has arcade physics and the player is moved with cursor keys: left player.setVelocityX(-160), right player.setVelocityX(160) and so on. The player and the platforms are colliding: this.physics.add.collider(player, platforms).

I tried to change the game to a 2D maze game, where the player moved left, right, up and down. I disabled the gravity and changed the key functionality:

player.x -= 2;
player.x += 2;
player.y -= 2;
player.y += 2;

Moving works well except for colliding with platforms: the player walks through them. Am I on the wrong track here?

Here is a link to the project: https://codesandbox.io/s/trusting-jepsen-d2f93?file=/index.html

You'll find the commented setVelocity lines there as well.

Upvotes: 2

Views: 796

Answers (1)

Kejender
Kejender

Reputation: 53

I found out from another forum that I should in fact use setVelocityX and setVelocityY commands for moving: collision won't work when setting the player.x and player.y values. This is what I was trying first, but I should have handled the events for stopping the character as well. When no keys are pressed, velocity values should be set to 0. I did have one handler but it was in the wrong place and moving worked in a strange way. I have updated the sandbox. 

Upvotes: 2

Related Questions