此代碼正確構建,一切似乎都正常,但密鑰無所作爲。我認爲它不是動作偵聽器就是橢圓形沒有更新。我正在嘗試通過初學者java遊戲編程工作。我相信這很容易,但我沒有抓住它。如果這有所作爲,我會在崇高的文本2中使用mac。KeyPressed事件沒有發生?
package javagame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JavaGame extends JFrame {
int x, y;
public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if(keycode == e.VK_LEFT); {
x-= 3;
}
if(keycode == e.VK_RIGHT); {
x+= 3;
}
if(keycode == e.VK_UP); {
y-= 3;
}
if(keycode == e.VK_DOWN); {
y+= 3;
}
}
public void keyReleased(KeyEvent e) {
}
}
public JavaGame() {
addKeyListener(new AL());
setTitle("Jave Game");
setSize(700, 700);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = 350;
y = 350;
}
public void paint(Graphics g) {
g.fillOval(x, y, 15, 15);
repaint();
}
public static void main(String[] args) {
new JavaGame();
}
}
如有疑問,請簡化問題。首先驗證你是通過'System.out.println'語句進入'keyPressed',然後按下右鍵,最後如果你正在更新繪製blob。 – PearsonArtPhoto
改爲使用KeyBindings – mKorbel