Skip to content

Fix game controls - keyboard and touch input now working properly#6

Merged
tsimiz merged 1 commit intomainfrom
copilot/fix-5
Jun 24, 2025
Merged

Fix game controls - keyboard and touch input now working properly#6
tsimiz merged 1 commit intomainfrom
copilot/fix-5

Conversation

Copy link
Contributor

Copilot AI commented Jun 24, 2025

Fixed critical controls issue where arrow keys, WASD keys, and touch controls were completely non-functional, preventing gameplay.

Issues Fixed

Touch Controls:

  • Touch events were using touch.clientX directly with canvas width, which are different coordinate systems
  • Fixed by using canvas.getBoundingClientRect() to properly calculate touch coordinates relative to the canvas
  • Added preventDefault() to touch events to prevent default browser behavior

Keyboard Controls:

  • Enhanced focus management by adding keyboard event listeners directly to the canvas element in addition to existing window listeners
  • Added click-to-focus functionality so the canvas properly receives keyboard input when clicked
  • Ensured both arrow keys (←/→/↑) and WASD keys work for movement and jumping

Technical Changes

// Before: Incorrect touch coordinate calculation
if (touch.clientX < width / 3) keys.left = true;

// After: Proper coordinate calculation
const rect = canvas.getBoundingClientRect();
const touchX = touch.clientX - rect.left;
const canvasWidth = canvas.width;
if (touchX < canvasWidth / 3) keys.left = true;
  • Added duplicate keyboard event listeners on canvas element for better focus handling
  • Enhanced focus management with click listener: canvas.addEventListener('click', () => canvas.focus())
  • Added proper preventDefault() calls to all touch events

Controls Now Working 🎮

  • Arrow Keys: ← and → for movement, ↑ for jumping
  • WASD Keys: A/D for movement, W for jumping
  • Touch/Mobile: Left third of screen moves left, right third moves right, center area makes character jump

Players can now properly control the character on both desktop and mobile devices. The character moves smoothly left and right, and jumps when grounded.

Fixes #5.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@tsimiz tsimiz marked this pull request as ready for review June 24, 2025 12:54
@tsimiz tsimiz merged commit 232855e into main Jun 24, 2025
3 checks passed
Copilot AI changed the title [WIP] Controls still not working Fix game controls - keyboard and touch input now working properly Jun 24, 2025
Copilot AI requested a review from tsimiz June 24, 2025 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Controls still not working

2 participants