From a81b496d4584e4e92afb57ef1b5faa8aa934fac0 Mon Sep 17 00:00:00 2001 From: Phil Norton Date: Mon, 17 Mar 2025 22:14:34 +0000 Subject: [PATCH] 20: Added wasd keys to the dirctions. Added a fix to prevent the keys from performing default actions. Also changed a coordinate variable to prevent eslint from complaining. --- four.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/four.js b/four.js index ab5fcaf..ff6c698 100644 --- a/four.js +++ b/four.js @@ -104,16 +104,16 @@ function setupGrid() { for (let i = 0; i < gridMaxX; i += 1) { grid[i] = []; column = 0; - const y = i * (width / gridMaxY); + const yCoord = i * (width / gridMaxY); for (let j = 0; j < gridMaxY; j += 1) { - const x = j * (width / gridMaxY); + const xCoord = j * (width / gridMaxY); const rectWidth = width / gridMaxY; const rectHeight = height / gridMaxY; grid[i][j] = { column: j, row: i, - x: x, - y: y, + x: xCoord, + y: yCoord, width: rectWidth, height: rectHeight, }; @@ -196,17 +196,22 @@ function keyPress(evt) { if (typeof userActionKeyPress !== 'function') { return; } + evt.preventDefault(); switch (evt.keyCode) { - case 37: + case 37: // Left arrow. + case 65: // "a". userActionKeyPress(KEYPRESS_LEFT); break; - case 38: + case 38: // Up arrow. + case 87: // "w". userActionKeyPress(KEYPRESS_UP); break; - case 39: + case 39: // Right arrow. + case 68: // "d". userActionKeyPress(KEYPRESS_RIGHT); break; - case 40: + case 40: // Down arrow. + case 83: // "s". userActionKeyPress(KEYPRESS_DOWN); break; default: