Skip to content

Commit

Permalink
fix: 레이싱 게임 스페이스바 이벤트 리스너를 document에 직접 걸지 않고 frontRef를 통해서 등록
Browse files Browse the repository at this point in the history
  • Loading branch information
minani-0621 committed Aug 23, 2024
1 parent 0901dcb commit caa79a4
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions Caecae/src/components/RacingGame/RacingGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,6 @@ const RacingGame: React.FC = () => {
}, fadeInterval);
};

const handleSpacebar = (event: KeyboardEvent) => {
if (event.code === "Space" && state.gameStatus === "playing" && !animationCompletedRef.current) {
// animationCompletedRef.current = true
event.preventDefault();
document.removeEventListener("keydown", handleSpacebar);

handleSmoothlyStop();
}
};

const handlePlayGame = () => {
stopingMusicReset();
playingMusicPlay();
Expand Down Expand Up @@ -228,15 +218,31 @@ const RacingGame: React.FC = () => {
};
}, []);

useEffect(() => {
if (state.gameStatus === "playing" && !animationCompletedRef.current) {
document.addEventListener("keydown", handleSpacebar);
} else {
const handleSpacebar = (event: KeyboardEvent) => {
if (event.code === "Space" && state.gameStatus === "playing" && !animationCompletedRef.current) {
event.preventDefault();
document.removeEventListener("keydown", handleSpacebar);
}

handleSmoothlyStop();
}
};

useEffect(() => {
const handleSpacebar = (event: KeyboardEvent) => {
if (event.code === "Space" && state.gameStatus === "playing" && !animationCompletedRef.current) {
event.preventDefault();
handleSmoothlyStop();
}
};

if (state.gameStatus === "playing" && frontRef.current) {
frontRef.current.addEventListener("keydown", handleSpacebar);
}

return () => {
document.removeEventListener("keydown", handleSpacebar);
if (frontRef.current) {
frontRef.current.removeEventListener("keydown", handleSpacebar);
}
};
}, [state.gameStatus]);

Expand Down

0 comments on commit caa79a4

Please sign in to comment.