Hello all,
First of all, i have been googling this issue quite much. And i have also read the api. But i cannot seem to find the solution. The following code is being used:
The problem with this code is really, that you cant start shooting, and while still having spacebar pressed, move.Code:public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_1){
selection = 1;
}
if (key == KeyEvent.VK_2){
selection = 2;
}
if (key == KeyEvent.VK_3){
selection = 3;
}
if (key == KeyEvent.VK_4){
selection = 4;
}
if (key == KeyEvent.VK_SPACE) {
if(timer<=0){
fire();
timer = selection*10;}
}
if (key == KeyEvent.VK_LEFT) {
direction = 'w';
dx = -1;
}
else if (key == KeyEvent.VK_RIGHT) {
direction = 'e';
dx = 1;
}
else if (key == KeyEvent.VK_UP) {
direction = 'n';
dy = -1;
}
else if (key == KeyEvent.VK_DOWN) {
direction = 's';
dy = 1;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
public void move() {
timer--;
previousX = x;
previousY = y;
x += dx;
y += dy;
if(x>320 && x<camera.mapLength-200)
camera.x+=dx;
if(y>240 && y<camera.mapHeight-200)
camera.y+=dy;
}
The second problem is, that if i walk left, then right(while still having left pressed) and release right again, that i will not move. Even though i have left still pressed.
The third problem is, it doesnt really work smooth. Pressing left, then quickly pressing right (right after i stop pressing left) will make the player stop moving. Even though i have right being pressed, I guess this is the same issue as the second problem.
Thanks in advance!
