【问题标题】:Multi-key Input [duplicate]多键输入[重复]
【发布时间】:2013-02-27 23:59:33
【问题描述】:

我有一个 2 人游戏,一个玩家使用 WASD,一个使用箭头键。我不知道如何一次允许多个键。这是我的代码:

List keyArray = new ArrayList();

    // Red guy input.
    if (e.getKeyCode() == 37) { // left key

        new LoadRedCharacter("leftrightfootred.gif");

        int x = Frame.redCharacterLabel.getX();
        int y = Frame.redCharacterLabel.getY();
        if (x < 0) {
            Frame.redHealthLabel.setLocation(x - 13, y - 15);
            Frame.redCharacterLabel.setLocation(x + 1, y);
            ResetEntities.redCharacterObj.setLocation(x + 1, y);
        } else {
            Frame.redHealthLabel.setLocation(x - 13, y - 15);
            Frame.redCharacterLabel.setLocation(x - 2, y);
            ResetEntities.redCharacterObj.setLocation(x - 2, y);
        }
        keyArray.add(37);
        System.out.println("array" + keyArray);
        Frame.frame.repaint();
        checkHitBox();
    }

我还有蓝色字符左移的代码。然后我有这个:

        // Multi key input
    if (keyArray.contains(37) && keyArray.contains(65)) {
        System.out.print("array contains 37 and 65");
    }

为了测试它。但是它不起作用..

【问题讨论】:

标签: java input key


【解决方案1】:

您可以采用多线程方法。有一个只关心红色键的线程和另一个只关心蓝色键的线程。如果您对共享变量使用同步,它们各自独立运行,不应相互干扰。

【讨论】:

    猜你喜欢
    • 2020-02-13
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2021-09-16
    相关资源
    最近更新 更多