hi,
what is the smoothest/fastest way of reacting to user keyboard input (in order to move the player) ?
Right now I’m doing it like this:
- user presses a key: the key which is pressed is remembered (for example: Globals.isMovingForward = true )
- a Thread which is always running in the backround checks the pressed keys and reacts by moving the player modell
But that approach is not smooth. It feels slow and its jerking (I tried different Thread priorities).
How do you do it?
Thanx, Usul
Some relevant examples of the way I do it:
class EventMouseKeyboard
implements KeyListener
{
public void keyPressed(KeyEvent e)
{
switch( e.getKeyCode() )
{
case KeyEvent.VK_W :
Globals.setMoveForward(true);
break;
...
}
}
public void keyReleased(KeyEvent e)
{
switch( e.getKeyCode() )
{
case KeyEvent.VK_W :
Globals.setMoveForward(false);
break;
}
}
}
public class Globals
{
private static boolean isMoveForward = false;
public static synchronized boolean getMoveForward()
{
return isMoveForward;
}
public static synchronized void setMoveForward(boolean isPressed)
{
isMoveForward = isPressed;
}
}
public class TheThread
extends Thread
{
private TransformGroup transformGroup;
private Transform3D transform3D;
private double moveRate = 0.2;
private double rotateAmount = Math.PI / 25.0;
TheThread(TransformGroup transformGroup)
{
this.transformGroup = transformGroup;
transform3D = new Transform3D();
setPriority(Thread.NORM_PRIORITY + 2);
}
public void run()
{
while(true)
{
try
{
Thread.sleep(30);
} catch (InterruptedException ex) {}
if( Globals.getMoveForward() )
{
moveForward();
}
}
}
}