How can I change this code so that the innerBox will slowly move towards the clicked…
- User
How can I change this code so that the innerBox will slowly move towards the clicked place?
import Head from "next/head"; import Image from "next/image"; import React, { useState, useEffect } from "react"; import { Leaderboard } from "../components/leaderboard.js";
import SwapPoolView from "../components/swapPoolView.js"; import StickyBoard from "../components/stickyNotes.js";
import leftImage from "../public/assets/left.gif"; import rightImage from "../public/assets/right.gif"; import idleImage from "../public/assets/idle.gif"; import downImage from "../public/assets/down.gif"; import upImage from "../public/assets/up.gif";
import worker from "../public/assets/worker.gif";
const BOX_HEIGHT = 600; const BOX_WIDTH = 800; const BOX_COLOR = "#ccc"; const INNER_BOX_SIZE = 70; const INNER_BOX_COLOR = "blue"; // const KEY_CODES = { // UP: 38, // LEFT: 37, // DOWN: 40, // RIGHT: 39, // }; const OBSTACLE_WIDTH = 75; const OBSTACLE_HEIGHT = 300;
const BOARD_WIDTH = 230; const BOARD_HEIGHT = 50;
export function Game() {
/////////////////// LOGIN////////////////// // const [showLogin, setShowLogin] = useState(true);
// const Login = () => { // return ( // <div style={{color:"white"}}className="container"> // Login // Please connect your wallet to continue // <button onClick={() => setShowLogin(false)}> Login // // ); // };
/////////////////////GAME CODE ////////////////////
const [innerBoxPosition, setInnerBoxPosition] = useState({ top: 500, left: 255, }); const [showDEX, setShowDEX] = useState(false); const [showBoard, setShowBoard] = useState(false);
const [showDEXText, setShowDEXText] = useState(false); const [showBoardText, setShowBoardText] = useState(false);
const [direction, setDirection] = useState("left");
const [collision, setCollision] = useState(false);
const [nekoText, setNekoText] = useState(false);
const [showNeko, setShowNeko] = useState(false);
const [isIdle, setIsIdle] = useState(true);
// useEffect(() => { // function handleKeyPress(event) { // const { keyCode } = event; // const { top, left } = innerBoxPosition; // const maxTop = BOX_HEIGHT - INNER_BOX_SIZE; // const maxLeft = BOX_WIDTH - INNER_BOX_SIZE; // const minTop = 0; // const minLeft = 0;
// switch (keyCode) { // case KEY_CODES.UP: // setInnerBoxPosition({ top: Math.max(minTop + 140, top - 10), left }); // setDirection("up"); // setIsIdle(false); // break; // case KEY_CODES.LEFT: // setInnerBoxPosition({ // top, // left: Math.max(minLeft + 185, left - 10), // }); // setDirection("left"); // setIsIdle(false); // break; // case KEY_CODES.DOWN: // setInnerBoxPosition({ top: Math.min(maxTop, top + 10), left }); // setDirection("down"); // setIsIdle(false); // break; // case KEY_CODES.RIGHT: // setInnerBoxPosition({ top, left: Math.min(maxLeft, left + 10) }); // setDirection("right"); // setIsIdle(false); // break; // default: // setIsIdle(true); // break; // } // }
- Assistant
- User
Now it doesnt move at all
- Assistant
Apologies for the confusion. I see that I made a small mistake in the
useEffecthook. Instead of adding a callback function that executesmoveInnerBoxin the dependency array, you need to add an empty dependency array[]so that the hook only runs once on mount.Here's the updated
useEffecthook:javascriptuseEffect(() => { moveInnerBox(); }, []);Please ensure you have made this correction and the innerBox should now move smoothly towards the clicked position. If the issue persists, please let me know.
- User
nothing changes
- Assistant
I apologize for the confusion. It appears there was a misunderstanding in my previous response. Please use
setTimeoutfunction inside themoveInnerBoxfunction to fix the issue. Here's the corrected implementation.Update the
moveInnerBoxfunction as follows:javascriptconst moveInnerBox = () => { const { top: currentTop, left: currentLeft } = innerBoxPosition; const { top: targetTop, left: targetLeft } = targetPosition; const lerpFactor = 0.1; const newTop = currentTop + (targetTop - currentTop) * lerpFactor; const newLeft = currentLeft + (targetLeft - currentLeft) * lerpFactor; setInnerBoxPosition({ top: newTop, left: newLeft }); // Call moveInnerBox with a delay using setTimeout setTimeout(moveInnerBox, 1000 / 60); // Approximately 60 frames per second };Now the innerBox should smoothly move to the clicked position.