我希望不使用基于事件的输入,而是轮询各种状态。

PS4控制器的映射..

public class PS4 {

    public static final int BUTTON_CROSS = 1;
    public static final int BUTTON_SQUARE = 0;
    public static final int BUTTON_TRIANGLE = 3;
    public static final int BUTTON_CIRCLE = 2;
    public static final int BUTTON_OPTIONS = 9;
    public static final int BUTTON_SHARE = 8;
    public static final int BUTTON_R1 = 5;
    public static final int BUTTON_R2 = 7;
    public static final int BUTTON_R3 = 11;
    public static final int BUTTON_L1 = 4;
    public static final int BUTTON_L2 = 6;
    public static final int BUTTON_L3 = 10;
    public static final int BUTTON_MOUSE = 13;
    public static final int BUTTON_PS = 12;

    public static final PovDirection BUTTON_DPAD_UP = PovDirection.north;
    public static final PovDirection BUTTON_DPAD_DOWN = PovDirection.south;
    public static final PovDirection BUTTON_DPAD_RIGHT = PovDirection.east;
    public static final PovDirection BUTTON_DPAD_LEFT = PovDirection.west;
}


游戏Screen的构造方法。

public GameScreen () {

    Controllers.addListener(this);

    if(Controllers.getControllers().size == 0) {

        setHasControllers(false);
        System.out.println("none"); // this is not printed

    } else {

        controller = Controllers.getControllers().first();
    }
}


在我的render()方法内部,我如何查看dpad上的按钮是否被按下?

if (controller.getPov(PovDirection.north)){

    // move character up

    }else if (controller.getPov(PovDirection.south)){
    // move character down
}

最佳答案

首先,请确保该控制器是PS4控制器。

我没有控制器,因此您可以在检测到字符串之前进行操作

if (controller.getName.toLowerCase().contains("ps4")) {
    ...
}


此外,在游戏类中,您需要实现接口ControllerListener。

public class Game implements ControllerListener {
    ...
}


例如,这是buttonDown的实现:

@Override
public boolean buttonDown(Controller controller, int buttonCode) {
    if (buttonCode == PS4.BUTTON_CROSS) {
        ...
    }
    return false;
}


另外,您应该检查被按下的控制器是否相同。

关于java - 使用gdx Controller 确定dpad上的按钮是否被按下,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31615315/

10-09 01:58