Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix] 레이싱 게임 스페이스바 이벤트리스너를 기존에 document에 걸었던 상태로 롤백 #69

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions Caecae/src/components/RacingGame/RacingGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ const RacingGame: React.FC = () => {
fetchData();
}

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

handleSmoothlyStop();
}
};

useEffect(() => {
const frontBackgroundImg = new Image();
frontBackgroundImg.src = frontBackground;
Expand All @@ -218,31 +227,15 @@ const RacingGame: React.FC = () => {
};
}, []);

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);
}
if (state.gameStatus === "playing" && !animationCompletedRef.current) {
document.addEventListener("keydown", handleSpacebar);
} else {
document.removeEventListener("keydown", handleSpacebar);
}

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

Expand Down
Loading