0

我正在编写一个适用于键盘的 Java 视频游戏。我还想添加操纵杆支持并找到方便的类JInput

然而(显然)与自动触发事件的键盘输入不同,从操纵杆/游戏手柄获取输入需要不断检查按钮按下事件。

因此,我编写了以下计时器任务,它每 50 毫秒搜索一次输入:

    Joystick_Input_Thread.schedule(new TimerTask() {
        @Override
        public void run() { 

            // If at least one controller was found we start firing action for the keys.
            try {
                if(!foundControllers.isEmpty()) {

                    clearControllerData(activeAction);
                    getControllerData(activeAction);

                }
            } catch (Exception e) {
                // Hmm.. crash due to initialization error probably
                System.out.println("JOYSTICK THREAD INITIALIZATION ERROR");
                this.cancel();
            }

        }
    }, 0, 50);

然而,正如预期的那样,这个类在某种程度上减慢了游戏的速度。我真的不想降低我运行这个类的频率,因为 50 毫秒对于我的快速动作游戏来说注册输入有点慢。

真正的问题是getControllerData()方法的权重。由于 JInput 的设计方式(除非我误解了什么),您必须遍历控制器上的每个按钮并检查每个按钮。

它看起来像这样:

// go through all components of the controller.
Component[] components = selectedController.getComponents();


// iterate through all the controls
for(Component c : components) {

    Component.Identifier componentIdentifier = c.getIdentifier();

    // check if the component is a BUTTON
    if(componentIdentifier.getName().matches("^[0-9]*$")) { 
       // check if it is pressed
       // ...
    } 

    // check if the component is a HAT SWITCH
    else if (componentIdentifier == Component.Identifier.Axis.POV) {
       // check if it has moved
       // ...
    }

    // check if the component is an AXES
    else if (component.isAnalog()) {
       // check if it has been activated
       // ...
    }

    // act on that...
}

它非常冗长,因为对于操纵杆/游戏手柄上的每个组件,我必须 A) 弄清楚它是什么,B) 弄清楚它是否被按下,最后 C) 触发动作事件。

所以我的问题是:总的来说,我怎样才能提高 jostick 输入的整体性能?

有没有更简单的方法来确定按钮是否被按下(而不是分析组件以确定它是什么类型,然后检查它是否被按下)?

我看到了频繁迭代的关键弱点。检查输入的合理刷新率是多少?有没有办法在没有所有循环的情况下接收 jinput?

最后,是否有任何其他 3rd 方 Java 类可以更好地处理此输入?

4

0 回答 0