Keyboard input for movement control gets stuck

Hi all,

I have a simple sketch that creates a player object (an ellipse) and moves the object around using the wasd keys. After moving the object around the screen a while though, it always gets stuck and further keyboard input no longer moves the object. Does anyone know why this is? I can't seem to find a pattern to it. The code for my main "gamePrototype" tab and my "Blob" class are below.

Thanks, Steve

Update: after playing with it more, I realized that after a little bit it will "snap out of it" and start functioning properly again, then will get stuck again. Sometimes only one of the directions will work and not others. I tried adding the capital letters to the keyPressed and keyReleased statements, but it did not solve the issue.

//This code is in the main tab called "gamePrototype"

boolean _up = false;  
boolean _down = false;  
boolean _left = false;  
boolean _right = false;  

Blob player;  

//Check Keyboard events  

void setup() {  
  size(640,480);  
  player = new Blob(width/2, height/2, 5, 5);  
}  

void keyPressed(){  
  if(key == 'w'){  
    _up = true;  
  }  
  if(key == 's'){  
    _down = true;  
  }  
  if(key == 'a'){  
    _left = true;  
  }  
  if(key == 'd'){  
    _right = true;  
  }  
}     

void keyReleased(){  
  if(key == 'w'){  
    _up = false;  
  }  
  if(key == 's') {  
    _down = false;  
  }  
  if(key == 'a'){  
    _left = false;  
  }  
  if(key == 'd'){  
    _right = false;  
  }  
}  


void draw(){  
  background(0);  
  updatePositions();  
  player.show();  
}     


void updatePositions() {  
       if(_up){  
         player.move(0, -player.speed);  
       }  
       if(_down){  
         player.move(0, player.speed);  
       }  
       if(_left){  
         player.move(-player.speed, 0);  
       }  
       if(_right){  
         player.move(player.speed, 0);  
       }   
}  


//The below code is in a separate tab called "Blob"  

class Blob {  
  float xpos;  
  float ypos;  
  float radius;  
  float speed;  

  Blob(float x, float y, float r, float s) {  
    xpos = x;  
    ypos = y;  
    radius = r;  
    speed = s;  
  }  

  void show() {  
    ellipse(xpos, ypos, radius*2, radius*2);  
  }  

  void move(float dx, float dy) {  
    xpos = xpos + dx;  
    ypos = ypos + dy;  
  }  

}  
Tagged:

Answers

  • Edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    Kf

  • Much better, thanks Kf!

  • Can you go down and left simultaneously?

  • Yep, I can do any of the diagonal directions fine until it gets stuck. If I'm pressing both left and right, or both up and down at the same time, there is no movement, which makes sense.

  • I don't know

  • Others have mentioned it works for them, so I'm thinking maybe it has something to do with my MacBook. I'll continue to investigate and try some other machines. Thanks for the help all.

  • I have the same issue! Super frustrating and I'd really appreciate some ideas for how to fix it!

    I'm sort of amazed that this issue isn't more clearly sorted, too, because I would have thought that there were loads of people who had tried to code games that needed to listen out for the switching on and off of multiple keys simultaneously. Weird bug. It's odd that the code clearly does work initially but then suddenly stops, and then starts working again.

    I also have a MacBook, on which the code does exhibit this problem.

    [update] HOWEVER, I've tried it out on my PC and the code works flawlessly. I mashed the keys as best I could but I couldn't get the code to NOT work - so it seems like this bug is a problem with the way that Processing has been implemented on the Mac. I'll leave a comment in the developer section of the processing.org website.

  • SOLVED! In macOS Sierra, Apple changed how key repeat works. I've linked the relevant section of the GitHub guidance. The solution is as quick and easy as could be : )

    https://github.com/processing/processing/wiki/Troubleshooting#key-repeat-on-macos-sierra

    Thanks Processing Team!!

  • @prfect23 Thank you for sharing your solution.

    Kf

  • No problem.

Sign In or Register to comment.