Platformer Player Ducking

@jjcapellan This is the answer!

Yes, here the way I solved the problem with @jjcapellan answer:

My platforms exist in a layer called groundLayer.
What I’m doing here is this:

function playerDucking() {

	walkingSpeed = 100;
	player.body.setSize(64, 32, false).setOffset(0, 32);

	if(!player.body.onFloor()) {

		state = states.FALLING;
	}

	if(cursors.down.isUp && !(groundLayer.hasTileAtWorldXY(player.body.position.x, player.body.position.y-1) || groundLayer.hasTileAtWorldXY(player.body.position.x+player.body.width, player.body.position.y-1))) {

		walkingSpeed = 200;
		player.body.setSize(64, 64, false).setOffset(0, 0);
		state = states.ON_GROUND;
	}
}

So, I check if the player has a tile above it, from the starting point of the sprite to the last pixel.
If so then the user can’t stand up.

Here the result:
image

Thanks a lot again.

1 Like