We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › input Reaction
Page Index Toggle Pages: 1
input Reaction (Read 1200 times)
input Reaction
May 31st, 2009, 10:58pm
 
I have a problem with the smoothness of the reaction when input keys are used it seems to stop for a sec and then go really fast. I made a prototype to demonstrate this problem and was wondering if anyone had a solution to making the inputs respond with more finesse.

int x,y;
void setup()
{
 size(400,400);
 x=width/2;
 y=height/2;
}

void draw()
{
 background(255);
 ellipse(x,y,20,20);
}

void keyPressed()
{
 if (key == CODED)
 {
   if (keyCode == UP)
   {
     y-=5;
   }
   else if (keyCode == DOWN)
   {
     y+=5;
   }
   else if (keyCode == LEFT)
   {
     x-=5;
   }
   else if (keyCode == RIGHT)
   {
     x+=5;
   }
 }
}
Re: input Reaction
Reply #1 - Jun 1st, 2009, 1:05am
 
First, thanks for making a simple code to demonstrate the issue. Sometime we are submerged with complex code, most of it unrelated to the issue... Smiley

I was wondering what you were talking about, until I pressed and hold an arrow key: we have one step down, a pause, and then a continuous move.
I fear that's the way your keyboard is coded in your system: that's the standard behavior of auto-repeat. Try that in a simple text area with a letter key.
To avoid this we would need to access the keyboard driver at a lower level, and I don't know if it is even possible in standard Java (while being portable). Might be interesting to see how Java games handle this, if they do.
Re: input Reaction
Reply #2 - Jun 1st, 2009, 2:05am
 
Since the initial keypress is reported immediately I think the answer is to store the state of the keys in booleans, switch these as and when a key event occurs and base movement on the state of the booleans rather than using Keypressed().  In fact here's a hastily thrown together adjustment to your code:

Code:
boolean left, right, up, down;

int x,y;
void setup()
{
size(400,400);
x=width/2;
y=height/2;
}

void draw()
{
background(255);
if (up) {
  y -=5;  
}
else if (down) {
  y +=5;  
}
if (left) {
  x -= 5;  
}
else if (right) {
  x +=5;  
}
ellipse(x,y,20,20);
}

void keyPressed() {
if (key == CODED)
{
  if (keyCode == UP) {
    up = true;
  }
  else if (keyCode == DOWN) {
    down = true;
  }
  else if (keyCode == LEFT) {
    left = true;
  }
  else if (keyCode == RIGHT) {
    right= true;
  }
}
}

void keyReleased() {
if (key == CODED)
{
  if (keyCode == UP) {
    up = false;
  }
  else if (keyCode == DOWN) {
    down = false;
  }
  else if (keyCode == LEFT) {
    left = false;
  }
  else if (keyCode == RIGHT) {
    right= false;
  }
}
}
Re: input Reaction
Reply #3 - Jun 1st, 2009, 3:00am
 
Excellent solution blindfish!
And it handles nicely diagonal movements too.
Re: input Reaction
Reply #4 - Jun 1st, 2009, 10:46am
 
thank you very much, i had considered something like that but wasn't sure how to go about it really.

it runs perfectly how i anticipated, i much appreciate your efforts.
Page Index Toggle Pages: 1