Skip to content

Commit

Permalink
Fix stop button functionality to properly stop the game loop in the 2…
Browse files Browse the repository at this point in the history
…-player Pong game.
  • Loading branch information
lovable-dev[bot] committed Jun 27, 2024
1 parent fa39df7 commit 920e8c0
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/pages/Index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ const Index = () => {
}
};

let animationFrameId;

const gameLoop = () => {
if (isGameRunning) {
draw();
update();
requestAnimationFrame(gameLoop);
animationFrameId = requestAnimationFrame(gameLoop);
}
};

Expand Down Expand Up @@ -117,6 +119,7 @@ const Index = () => {

return () => {
window.removeEventListener("keydown", handleKeyDown);
cancelAnimationFrame(animationFrameId);
};
}, [isGameRunning]);

Expand Down

0 comments on commit 920e8c0

Please sign in to comment.