Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
IF statement in Switch React JS

I'm making a snake game in React and it works fine but I have defined a switch case, where you can control the snake with the arrow keys. But there's a problem, when the snake is going to one direction I don't want it to be able to go to the opposite direction. as in prevent changing direction along same axis, ie left if currently moving right, down if currently moving up

Now I'm wondering how can I define an If statement in my switch case for it? My code so far looks like this :

 //handle user input
  const handleKeyPress = (e) => {
e = e || window.event;
setStarted(true);
switch (e.keyCode) {
  case 38:
    setDirection('UP');
    break;
  case 40:
    setDirection('DOWN');
    break;
  case 37:
    setDirection('LEFT');
    break;
  case 39:
    setDirection('RIGHT');
    break;
  default:
    break;
  }
  };

 //moving the snake
 const moveSnake = () => {
//take temporary positions
let tempSnakePosition;
let dir;
//get fresh cordinates
setSnakeCordinates((prev) => {
  tempSnakePosition = [...prev];
  return prev;
});
setDirection((prev) => {
  dir = prev;
  return prev;
});
//find head
let tempHead = tempSnakePosition[tempSnakePosition.length - 1];
//change head
let l = tempHead.left;
let t = tempHead.top;
switch (dir) {
  case 'RIGHT':
    if ('LEFT' && 'UP')
      l = tempHead.left + size

    break;
  case 'LEFT':
    if ('RIGHT')
      l = tempHead.left - size;
    break;
  case 'DOWN':
    t = tempHead.top + size;
    break;
  case 'UP':
    t = tempHead.top - size;
    break;
  default:
    break;
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

const [prevDir, setPrevDir] = useState('');  

const handleKeyPress = (e) => {
    switch (e.keyCode) {
        case 38:
            if(prevDir !== 'DOWN') {
                setDirection('UP'); 
            }
            break;
        case 40:
            if(prevDir !== 'UP') {
                setDirection('DOWN');
                break;
            }
        case 37:
            if(prevDir !== 'RIGHT') {
                setDirection('LEFT');
            }
            break;
        case 39:
            if(prevDir !== 'LEFT') {
                setDirection('RIGHT');
                break;
            }
        default:
            break;
      }
}

setDirection((prev) => {
    dir = prev;
    setPrevDir(prev);
    return prev;
 });
over 4 years ago · Santiago Trujillo Report

0

Try using you dir variable like this:

//handle user input
const handleKeyPress = (e) => {
e = e || window.event;
setStarted(true);
switch (e.keyCode) {
  case 38:
    if(dir !== 'DOWN')
      setDirection('UP');
    break;
  case 40:
    if(dir !== 'UP')
      setDirection('DOWN');
    break;
  case 37:
    if(dir !== 'RIGHT')
      setDirection('LEFT');
    break;
  case 39:
    if(dir !== 'LEFT')
      setDirection('RIGHT');
    break;
  default:
    break;
  }
};
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!