What I want is that press the " A", " S ", "D " and " W " the character move in their respective directions keys.
The problem is that every time I change direction , I change the image of JLabel ( it is an arrow ) .
It really works well but every time the image is changed, the JLabel back for a second to its default position . Then the JLabel continues from where it was.
Here is the code.
private int pX;
private int pY;
public MovePj() {
initComponents();
pX=labelPj.getX();
pY=labelPj.getY(); }
private void formKeyPressed(KeyEvent evt) {
switch(evt.getKeyCode()){
case 87: //Norte
pY=pY-movimiento;
labelPersonaje.setIcon(new ImageIcon(rutaBase+"Imagenes\\movement\\07-north.png"));
break;
case 83: //Sur
pY=pY+movimiento;
labelPersonaje.setIcon(new ImageIcon(rutaBase+"Imagenes\\movement\\03-sur.png"));
break;
case 68: //Este
pX=pX+movimiento;
labelPersonaje.setIcon(new ImageIcon(rutaBase+"Imagenes\\movement\\01-east.png"));
break;
case 65: //Oeste
pX=pX-movimiento;
labelPersonaje.setIcon(new ImageIcon(rutaBase+"Imagenes\\movement\\05-west.png"));
break;
default:
break;
}
labelPersonaje.setLocation(pX, pY);
//labelPj.setBounds(pX, pY, labelPj.getWidth(), labelPj.getHeight());
}
The character moves correctly, but to change the image of JLabel , this returns to the preset point.
Thanks.