// Erase the canvas and draw all the objects.function draw() { var canvas = document.getElementById("stage"); var ctx = canvas.getContext("2d"); erase(ctx); draw_player(ctx); draw_player1(ctx);}
// Handle input and move the player.// Handle input and move player 1.function update_player() {function update_player1() { check_input(); // Move the player to the new location. _player.x += _player.velocity_x; _player.y += _player.velocity_y; _player1.x += _player1.velocity_x; _player1.y += _player1.velocity_y;}
script.js:
function check_input() { _player.velocity_x = 0; _player.velocity_y = 0; _player1.velocity_x = 0; _player1.velocity_y = 0; // Left arrow or 'a' to move left. if (_game.keymap[37] || _game.keymap[65]) { _player.velocity_x = -1; _player1.velocity_x = -1; } // Right arrow or 'd' to move right. if (_game.keymap[39] || _game.keymap[68]) { _player.velocity_x = 1; _player1.velocity_x = 1; } // Up arrow or 'w' to move up. if (_game.keymap[38] || _game.keymap[87]) { _player.velocity_y = -1; _player1.velocity_y = -1; } // Down arrow or 's' to move up. if (_game.keymap[40] || _game.keymap[83]) { _player.velocity_y = 1; _player1.velocity_y = 1; }}
script.js:
// This is called ~60 times per second to update the world.function update_world() { update_player(); update_player1(); draw(); requestAnimationFrame(update_world);}