Ich kann nicht direkt auf Keylistener gehenJava

Java-Forum
Anonymous
 Ich kann nicht direkt auf Keylistener gehen

Post by Anonymous »

Ich mache Platzinvader für ein Schulprojekt und wenn ich versuche, den D -Schlüssel zu verwenden, um es richtig zu machen

Code: Select all

public PannelloGioco() {
setBackground(Color.BLACK);
navicella = new Navicella(350, 500); // Posizione iniziale della navicella

//metodo per gestire gli input da tastiera
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_D) {
navicella.Sinistra();
} else if (keyCode == KeyEvent.VK_A) {
navicella.Dstra();
} else if (keyCode == KeyEvent.VK_W) {
navicella.Sopra();
} else if (keyCode == KeyEvent.VK_S) {
navicella.Sotto();
}
repaint();  // faccio il repaint dopo ogni azione
}
});

// Aziono il focus per poter ricevere gli eventi da tastiera
setFocusable(true);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);  // Pulisce il pannello
navicella.draw(g);  // Disegna la navicella
}`
< /code>
Und dies ist die Logik, um es zu bewegen. < /p>
public void Sinistra() {
//metodo per spostare la navicella a sinistra
if (x > 0) {
x -= speed+10;
}
}

public void Dstra() {
//metodo per spostare la navicella a destra
if (x + larghezza > 800) {
x -= speed+10;
}
}

public void Sopra() {
if (y > 0) {
//metodo per spostare la navicella sopra
y -= speed+10;
}
}

public void Sotto() {
if (y + altezza < 600) {
//metodo per spostare la navicella sotto
y += speed+10;
}
}
idk Was ist zu tun, um es richtig zu machen>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post