073 : Fixing the platforms

Balthazar:

Now that we have the collision info, we can fix up the platforms.

Balthazar:

To do that we need to change how we handle platform collisions

function check_platform_collisions() { var platforms = _game.platforms; _player.platform = null; for (var i = 0; i < platforms.length; i++) { if (collide(platforms[i], _player)) { _player.platform = platforms[i]; _player.y = platforms[i].y; _player.velocity_y = 0; var overlap = collide(platforms[i], _player); if (overlap) { // Adjust player location so it no longer overlaps with platform. _player.x += overlap[0]; _player.y += overlap[1]; var dir = overlap[2]; if (dir == "left" || dir == "right") { // Smack into left/right side of platform. _player.velocity_x = 0; } else if (dir == "top") { // Land on top of platform. _player.platform = platforms[i]; _player.velocity_y = 0; } else { // Bounce off bottom of platform. _player.velocity_y *= -1; } } } }

Congratulations! You've earned the Platform II - Four Sided badge!

GOTO 074