the key input works however there is a delay when more than two keys are pressed at once. Also there is a 1 second delay when switching directions. I was wondering how I could fix this issue?
public void keyPressed( KeyEvent ke )
{
switch(ke.getKeyCode()) {
case KeyEvent.VK_DOWN: spaceship.ypos+=12; break;
case KeyEvent.VK_UP: spaceship.ypos-=12; break;
case KeyEvent.VK_LEFT: spaceship.xpos-=12; break;
case KeyEvent.VK_RIGHT: spaceship.xpos+=12; break;
}
repaint();
}
If more code is needed to understand what I am talking about, I can add more.\
Edit: I fixed my problem by adding a main class with a thread that constantly checks if the booleans are active so there was no delay.
public class MainLoop implements Runnable{
public MainLoop(){
}
public void run(){
while(true){
if(up){ spaceship.ypos-=8; }
if(down){ spaceship.ypos+=8; }
if(left){ spaceship.xpos-=8; }
if(right){ spaceship.xpos+=8; }
repaint();
try {
Thread.sleep(20);
}
catch (InterruptedException ex){
}
}
}
}`