我刚开始学习如何使用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;
}
}
}
我正在尝试输出它会在控制台中向上移动,以便我可以倾斜如何制作新的KeyBinding但我不知道为什么它不起作用。任何解决方案/提示将不胜感激。
P.S。我是Java新手,对于简单的错误感到抱歉,我也知道主游戏窗口类中的狂野循环。
编辑:在单独的计时器类中每隔几毫秒调用一次move()。
答案 0 :(得分:3)
KeyStroke.getKeyStroke("W")
并不能按照您的想法行事。使用此表单需要详细描述操作,即typed w
。
出于这个原因,我从不使用它。相反,我更喜欢使用更直接的KeyStroke.getKeyStroke(KeyEvent.VK_W, 0)
有关详细信息,请参阅KeyStroke#getKeyStroke(int, int)
。
您还没有绑定关键笔划,因为您从未调用move
方法。相反,绑定类构造函数中的键击或其他应该只调用一次的方法。