118 : Giving direction

Balthazar:

and fixup the input:

function check_input() { // Left arrow or 'a' to move left. if (_game.keymap[37] || _game.keymap[65]) { _player.dir = -1; _player.velocity_x -= _player.velocity_x_delta; if (_player.velocity_x < -_player.velocity_x_max) { _player.velocity_x = -_player.velocity_x_max; } } // Right arrow or 'd' to move right. if (_game.keymap[39] || _game.keymap[68]) { _player.dir = 1; _player.velocity_x += _player.velocity_x_delta; if (_player.velocity_x > _player.velocity_x_max) { _player.velocity_x = _player.velocity_x_max; } } ... }
Balthazar:

fix the player drawing

function draw_player(ctx) { var sprite = update_player_sprite(); set_transform(ctx, _player); ctx.drawImage(sprite.img, _player.x - _player.origin_x, _player.y - _player.origin_y); ctx.drawImage(sprite.img, 0, 0); reset_transform(ctx); }
Balthazar:

and now do the same thing for monsters

function create_monster(x, y, width, height, min_x, max_x, move_x, image) { ... m.min_x = min_x; m.max_x = max_x; m.move_x = move_x; m.dir = (move_x > 0) ? 1 : -1; m.img = new Image(); ... }
Balthazar:

and we we need to adjust the code that moves the monsters around.

function update_monsters() { var level = _levels[_game.current_level]; var monsters = level.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; m.dir *= -1; } } }
Balthazar:

and the code that deaws the monsters

function draw_monsters(ctx) { var level = _levels[_game.current_level]; var monsters = level.monsters; for (var i = 0; i < monsters.length; i++) { var m = monsters[i]; set_transform(ctx, m); ctx.drawImage(m.img, m.x - m.origin_x, m.y - m.origin_y); ctx.drawImage(m.img, 0, 0); reset_transform(ctx); } }

Congratulations! You've earned the Sprite III - Direction badge!

GOTO 100