[英]My KeyBindings don't work and I want to know why
我剛開始學習如何使用keyBindings,我無法找到我做錯的事情,因為當我按下鍵盤上的向上箭頭時它什么也沒做。
我的主要游戲窗口
public class GameWindow extends JFrame{
private static final long serialVersionUID = 1L;
public int WIDTH = 160, HEIGHT = WIDTH/12 *9, SCALE = 3;
public boolean running = false;
BackGround bg = new BackGround();
Ranger R = new Ranger();
TimerClass T = new TimerClass();
public static void main(String[] args) {
new GameWindow();
}
public GameWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH * SCALE, HEIGHT * SCALE);
setResizable(false);
running = true;
add(bg);
bg.add(R);
bg.setFocusable(true);
R.setFocusable(true);
setFocusable(true);
setVisible(true);
bg.repaint();
run();
}
public void run() {
while (running) {
render();
}
}
public void render() {
bg.setLocation(Ranger.bgX, Ranger.bgY);
R.setLocation(Ranger.X, Ranger.Y);
R.setIcon(Ranger.rangerA[Ranger.I]);
R.repaint();
bg.repaint();
}
}
和我的游俠班
public class Ranger extends JLabel {
private static final long serialVersionUID = 1L;
public static int X, Y, dX, dY, bgX, bgY, I = 0, jumpTime = 100;
public static boolean moving = false, movingLeft = false,
movingRight = false, onFloor = false, jumping = false,
movingUp = false, movingDown = false;
public int totalImages = 6;
public BufferedImage ranger1, ranger2, ranger3, ranger4, ranger5, ranger6;
public static ImageIcon[] rangerA;
static TileMap TileMap = new TileMap();
public Ranger() {
try {
// not moving
ranger1 = ImageIO.read(getClass().getResource(
"/Images/Sprites/ranger/Ranger0.png"));
ranger2 = ImageIO.read(getClass().getResource(
"/Images/Sprites/ranger/Ranger1.png"));
// moving Left
ranger3 = ImageIO.read(getClass().getResource(
"/Images/Sprites/ranger/Ranger2.png"));
ranger4 = ImageIO.read(getClass().getResource(
"/Images/Sprites/ranger/Ranger3.png"));
// moving Right
ranger5 = ImageIO.read(getClass().getResource(
"/Images/Sprites/ranger/Ranger4.png"));
ranger6 = ImageIO.read(getClass().getResource(
"/Images/Sprites/ranger/Ranger5.png"));
} catch (IOException e) {
e.printStackTrace();
}
array();
}
public void array() {
rangerA = new ImageIcon[6];
{
rangerA[0] = new ImageIcon(ranger1);
rangerA[1] = new ImageIcon(ranger2);
rangerA[2] = new ImageIcon(ranger3);
rangerA[3] = new ImageIcon(ranger4);
rangerA[4] = new ImageIcon(ranger5);
rangerA[5] = new ImageIcon(ranger6);
}
}
public void move() {
AbstractAction moveUp = new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
System.out.println("Move up");
}
};
this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "moveUp");
this.getActionMap().put("moveUp", moveUp);
X += dX;
Y += dY;
dX = 0;
dY = 0;
if (movingRight || movingLeft) {
moving = true;
}
}
}
我正在嘗試輸出它將在控制台中向上移動,以便我可以傾斜如何制作新的KeyBindings但我不知道為什么它不起作用。 任何解決方案/提示將不勝感激。
PS我是Java新手,對於簡單的錯誤很抱歉,我也知道主游戲窗口類中的狂野循環。
編輯:在單獨的計時器類中每隔幾毫秒調用move()。
KeyStroke.getKeyStroke("W")
不會按照您的想法執行操作。 使用此表單需要詳細描述操作,即typed w
。
出於這個原因,我從不使用它。 相反,我更喜歡使用更直接的KeyStroke.getKeyStroke(KeyEvent.VK_W, 0)
有關更多詳細信息KeyStroke#getKeyStroke(int, int)
請參閱KeyStroke#getKeyStroke(int, int)
。
你也沒有綁定擊鍵,因為你從來沒有調用move
方法。 相反,綁定類構造函數中的鍵擊或其他應該只調用一次的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.