075 : More platforms

function draw_platforms(ctx) { var platform = _game.platform; var platform = _game.platforms[0]; ctx.fillStyle = "rgb(153, 102, 51)"; ctx.fillRect(platform.x, platform.y, platform.width, platform.height); }
function check_platform_collisions() { _player.platform = null; if (collide(_game.platform, _player)) { if (collide(_game.platforms[0], _player)) { _player.platform = _game.platform; _player.platform = _game.platforms[0]; _player.y = _game.platform.y; _player.y = _game.platforms[0].y; _player.velocity_y = 0; } }
Balthazar:

Run. it should be the same.

Balthazar:

Note that draw_ and check_collision only check the first (the 0th) element) of the array.

Balthazar:

We can add another platform to the array, but it won't work until we iterate through all the elements of the array. WE do that with a for loop.

function draw_platforms(ctx) { var platforms = _game.platforms; for (var i = 0; i < platforms.length; i++) { var platform = _game.platforms[0]; var platform = platforms[i]; ctx.fillStyle = "rgb(153, 102, 51)"; ctx.fillRect(platform.x, platform.y, platform.width, platform.height); } }
Balthazar:

_game.platforms.length is the number of elements in the array.

Balthazar:

Note that _game.platforms[0] is changed to _game.platforms[i].

function check_platform_collisions() { var platforms = _game.platforms; _player.platform = null; for (var i = 0; i < platforms.length; i++) { if (collide(_game.platforms[0], _player)) { if (collide(platforms[i], _player)) { _player.platform = _game.platforms[0]; _player.platform = platforms[i]; _player.y = _game.platforms[0].y; _player.y = platforms[i].y; _player.velocity_y = 0; } } }

RUN your code in a browser and verify that it loads without errors.

GOTO 079 if you already have the Monster I - Stationary badge.

GOTO 071