Keyboard Movement

With just a little more work you can build a simple system to control a sprite using the keyboard. To simplify your code, I suggest you use this custom function called keyboard that listens for and captures keyboard events.

  1. function keyboard(value) {
  2. let key = {};
  3. key.value = value;
  4. key.isDown = false;
  5. key.isUp = true;
  6. key.press = undefined;
  7. key.release = undefined;
  8. //The `downHandler`
  9. key.downHandler = event => {
  10. if (event.key === key.value) {
  11. if (key.isUp && key.press) key.press();
  12. key.isDown = true;
  13. key.isUp = false;
  14. event.preventDefault();
  15. }
  16. };
  17. //The `upHandler`
  18. key.upHandler = event => {
  19. if (event.key === key.value) {
  20. if (key.isDown && key.release) key.release();
  21. key.isDown = false;
  22. key.isUp = true;
  23. event.preventDefault();
  24. }
  25. };
  26. //Attach event listeners
  27. const downListener = key.downHandler.bind(key);
  28. const upListener = key.upHandler.bind(key);
  29. window.addEventListener(
  30. "keydown", downListener, false
  31. );
  32. window.addEventListener(
  33. "keyup", upListener, false
  34. );
  35. // Detach event listeners
  36. key.unsubscribe = () => {
  37. window.removeEventListener("keydown", downListener);
  38. window.removeEventListener("keyup", upListener);
  39. };
  40. return key;
  41. }

The keyboard function is easy to use. Create a new keyboard object like this:

  1. let keyObject = keyboard(keyValue);

Its one argument is the key value that you want to listen for. Here’s a list of keys.

Then assign press and release methods to the keyboard object like this:

  1. keyObject.press = () => {
  2. //key object pressed
  3. };
  4. keyObject.release = () => {
  5. //key object released
  6. };

Keyboard objects also have isDown and isUp Boolean properties that you can use to check the state of each key.

Don’t forget to remove event listeners by using the unsubscribe method :

  1. keyObject.unsubscribe();

Take a look at the keyboardMovement.html file in the examples folder to see how you can use this keyboard function to control a sprite using your keyboard’s arrow keys. Run it and use the left, up, down, and right arrow keys to move the cat around the stage.

Keyboard movement

Here’s the code that does all this:

  1. //Define any variables that are used in more than one function
  2. let cat, state;
  3. function setup() {
  4. //Create the `cat` sprite
  5. cat = new Sprite(resources["images/cat.png"].texture);
  6. cat.y = 96;
  7. cat.vx = 0;
  8. cat.vy = 0;
  9. app.stage.addChild(cat);
  10. //Capture the keyboard arrow keys
  11. let left = keyboard("ArrowLeft"),
  12. up = keyboard("ArrowUp"),
  13. right = keyboard("ArrowRight"),
  14. down = keyboard("ArrowDown");
  15. //Left arrow key `press` method
  16. left.press = () => {
  17. //Change the cat's velocity when the key is pressed
  18. cat.vx = -5;
  19. cat.vy = 0;
  20. };
  21. //Left arrow key `release` method
  22. left.release = () => {
  23. //If the left arrow has been released, and the right arrow isn't down,
  24. //and the cat isn't moving vertically:
  25. //Stop the cat
  26. if (!right.isDown && cat.vy === 0) {
  27. cat.vx = 0;
  28. }
  29. };
  30. //Up
  31. up.press = () => {
  32. cat.vy = -5;
  33. cat.vx = 0;
  34. };
  35. up.release = () => {
  36. if (!down.isDown && cat.vx === 0) {
  37. cat.vy = 0;
  38. }
  39. };
  40. //Right
  41. right.press = () => {
  42. cat.vx = 5;
  43. cat.vy = 0;
  44. };
  45. right.release = () => {
  46. if (!left.isDown && cat.vy === 0) {
  47. cat.vx = 0;
  48. }
  49. };
  50. //Down
  51. down.press = () => {
  52. cat.vy = 5;
  53. cat.vx = 0;
  54. };
  55. down.release = () => {
  56. if (!up.isDown && cat.vx === 0) {
  57. cat.vy = 0;
  58. }
  59. };
  60. //Set the game state
  61. state = play;
  62. //Start the game loop
  63. app.ticker.add(delta => gameLoop(delta));
  64. }
  65. function gameLoop(delta){
  66. //Update the current game state:
  67. state(delta);
  68. }
  69. function play(delta) {
  70. //Use the cat's velocity to make it move
  71. cat.x += cat.vx;
  72. cat.y += cat.vy
  73. }