141 : If goal

Balthazar:

If you run the code now, the game will stop when you reach the third level. This is because we have code that assumes that each level has a 'goal' object. Since this is no longer the case, it will throw an exception (error) and stop when you get to a level without a goal (level 3 in our case).

Balthazar:

We can fix this by making the code work by having the draw_goal() and check_goal_collisions() functions check to make sure it has a valid goal object before trying to draw it.

function draw_goal(ctx) { var level = _levels[_game.current_level]; var goal = level.goal; if (goal) { ctx.fillStyle = "green"; ctx.fillRect(goal.x - goal.origin_x, goal.y - goal.origin_y, goal.width, goal.height); } }
function check_goal_collisions() { var level = _levels[_game.current_level]; var goal = level.goal; if (goal) { if (collide(goal, _player) && level.player_has_key) { start_level(goal.next_level); } } }

GOTO 138