gpt4 book ai didi

java - 我的 KeyBindings 不起作用,我想知道为什么

转载 作者:行者123 更新时间:2023-11-29 04:55:33 26 4
gpt4 key购买 nike

我刚刚开始学习如何使用 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,但我不知道为什么它不起作用。任何解决方案/提示将不胜感激。

附言我是 Java 的新手,很抱歉犯了一些简单的错误,我也知道主游戏窗口类中的疯狂循环。

编辑:在单独的计时器类中每隔几毫秒调用一次 move()。

最佳答案

KeyStroke.getKeyStroke("W") 并不如您所想。使用此表单需要对操作进行详细描述,即 typed w

因此,我从不使用它。相反,我更喜欢使用更直接的 KeyStroke.getKeyStroke(KeyEvent.VK_W, 0)

参见 KeyStroke#getKeyStroke(int, int)了解更多详情。

您也没有绑定(bind)击键,因为您从不调用 move 方法。相反,将击键绑定(bind)到类构造函数或其他一些只应调用一次的方法中。

关于java - 我的 KeyBindings 不起作用,我想知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33902704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com