簡體   English   中英

使用gdx控制器確定dpad上的按鈕是否被按下

[英]Determine if button on dpad is pressed using gdx-controllers

我希望不使用基於事件的輸入,而是輪詢各種狀態。

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;
}

另外,您應該檢查被按下的控制器是否相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM