使用gdx-controllers

时间:2015-07-24 16:18:37

标签: java libgdx

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

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
}

1 个答案:

答案 0 :(得分:1)

首先,确保控制器 是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;
}

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