061 : Moving monsters

Balthazar:

Let's make the monsters move.

function draw_player(ctx) { ... } function update_monsters() { var monsters = _game.monsters; for (var i = 0; i < monsters.length; i++) { var m = monsters[i]; m.x += m.move_x; if (m.x <= m.min_x || m.x >= m.max_x) { m.move_x *= -1; } } } // Handle input and move the player. function update_player() { ... }
Balthazar:

fdfafd

function update_world() { update_monsters(); update_player(); check_collisions(); draw(); requestAnimationFrame(update_world); }
Balthazar:

We need to initialize these values:

function create_monster(x, y, width, height) { function create_monster(x, y, width, height, min_x, max_x, move_x) { var m = {}; m.x = x; m.y = y; m.width = width; m.height = height; m.origin_x = m.width / 2; m.origin_y = m.height; m.min_x = min_x; m.max_x = max_x; m.move_x = move_x; return m; }
Balthazar:

in init_game()

function init_game() { ... _game.monsters = []; _game.monsters.push(create_monster(350, 360, 20, 20)); _game.monsters.push(create_monster(350, 360, 20, 20, 60, 470, -1.0)); _game.monsters.push(create_monster(400, 360, 20, 20)); _game.monsters.push(create_monster(400, 360, 20, 20, 60, 470, 0.8)); ... }

Congratulations! You've earned the Monster II - Roaming badge!

GOTO 089 if you already have the Platform II - Four Sided badge.

GOTO 070