Ich habe 2 Klassen namens Game.java
und KeyInput.java
. Wie greife ich auf int x und int y von Game.java
und verwenden Sie in KeyInput.java
?Zugriff auf Variablen aus einer anderen Klasse
Game.java
public class Game extends JFrame {
int x, y;
//Constructor
public Game(){
setTitle("Game");
setSize(300, 300);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyInput());
x = 150;
y = 150;
}
public void paint(Graphics g){
g.fillRect(x, y, 15, 15);
}
public static void main(String [] args){
new Game();
}
}
KeyInput.java
public class KeyInput extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == e.VK_W)
y--; //Error here saying "y cannot be resolved to a variable"
}
}
vielen Dank! – Sean
Hey, kein Problem! :) – ldmtwo