From 29b0e980fe9f02dd333d239f1bc661d02aa48560 Mon Sep 17 00:00:00 2001 From: minani-0621 Date: Fri, 23 Aug 2024 20:45:26 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=A0=88=EC=9D=B4=EC=8B=B1=20=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20=EC=8A=A4=ED=8E=98=EC=9D=B4=EC=8A=A4=EB=B0=94=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=EB=A6=AC=EC=8A=A4=EB=84=88=EB=A5=BC?= =?UTF-8?q?=20=EA=B8=B0=EC=A1=B4=EC=97=90=20document=EC=97=90=20=EA=B1=B8?= =?UTF-8?q?=EC=97=88=EB=8D=98=20=EC=83=81=ED=83=9C=EB=A1=9C=20=EB=A1=A4?= =?UTF-8?q?=EB=B0=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/RacingGame/RacingGame.tsx | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/Caecae/src/components/RacingGame/RacingGame.tsx b/Caecae/src/components/RacingGame/RacingGame.tsx index 06034ef..153b1ca 100644 --- a/Caecae/src/components/RacingGame/RacingGame.tsx +++ b/Caecae/src/components/RacingGame/RacingGame.tsx @@ -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; @@ -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]);