處理控制器動作

在系統層級,Android 會將遊戲控制器的輸入事件代碼以 Android 按鍵碼和軸值回報。您可以在遊戲中接收這些代碼和值,並將這些代碼和值轉換為特定遊戲內動作。

當玩家實際連線,或以無線方式將遊戲控制器與 Android 裝置配對時,系統會將控制器自動偵測為輸入裝置,並開始回報其輸入事件。遊戲可以接收這些輸入事件,方法是在有效的 Activity 或聚焦的 View 中實作下列回呼方法 (您應為 ActivityView 實作回呼,但不能同時實作兩者):

建議的做法是從使用者互動的特定 View 物件擷取事件。檢查回呼提供的下列物件,以取得接收的輸入事件類型相關資訊:

KeyEvent
這個物件描述方向鍵 (D-Pad) 和遊戲手把按鈕事件。按鍵事件會附帶按鍵代碼,用來指出觸發的特定按鈕,例如 DPAD_DOWNBUTTON_A。您可以呼叫 getKeyCode() 或從重要事件回呼 (例如 onKeyDown()) 取得按鍵代碼。
MotionEvent
這個物件用於說明搖桿和肩膀觸發動作的輸入方式。動作事件會隨附一個動作程式碼和一組軸值。動作程式碼會指定發生的狀態變更,例如搖桿移動。軸值會描述特定實體控制項的位置和其他移動屬性,例如 AXIS_XAXIS_RTRIGGER。如要取得動作程式碼,請呼叫 getAction() 和軸值,方法是呼叫 getAxisValue()

本課程著重於實作上述的 View 回呼方法,以及處理 KeyEventMotionEvent 物件,以便處理遊戲畫面常見類型實體控制項 (遊戲手把按鈕、方向鍵及搖桿) 的輸入內容。

確認遊戲控制器已連線

回報輸入事件時,Android 不會區別來自非遊戲控制器裝置的事件,以及來自遊戲控制器的事件。舉例來說,觸控螢幕動作會產生代表觸控表面的 X 座標的 AXIS_X 事件,但搖桿會產生 AXIS_X 事件,代表搖桿的 X 位置。如果遊戲需要處理遊戲控制器的輸入內容,請先檢查輸入事件是否來自相關來源類型。

如要驗證已連結的輸入裝置是否為遊戲控制器,請呼叫 getSources() 取得該裝置所支援的輸入來源類型的合併位元欄位。接著,您可以測試檢查是否已設定下列欄位:

  • SOURCE_GAMEPAD 的來源類型表示輸入裝置有遊戲手把按鈕 (例如 BUTTON_A)。請注意,這個來源類型無法明確指出遊戲控制器是否具有 D-Pad 按鈕,雖然大多數遊戲控制器通常都有方向控制項。
  • SOURCE_DPAD 來源類型表示輸入裝置有 D-Pad 按鈕 (例如 DPAD_UP)。
  • SOURCE_JOYSTICK 的來源類型表示輸入裝置具有類比控制桿 (例如記錄 AXIS_XAXIS_Y 沿途運動的搖桿)。

下列程式碼片段為輔助方法,可讓您檢查已連結的輸入裝置是否為遊戲控制器。如果是,這個方法會擷取遊戲控制器的裝置 ID。接著,您可以將每個裝置 ID 與遊戲中的玩家建立關聯,然後分別為每個已連結的玩家處理遊戲動作。如要進一步瞭解如何支援多個同時連線至同一 Android 裝置的遊戲控制器,請參閱「支援多個遊戲控制器」。

Kotlin

fun getGameControllerIds(): List<Int> {
    val gameControllerDeviceIds = mutableListOf<Int>()
    val deviceIds = InputDevice.getDeviceIds()
    deviceIds.forEach { deviceId ->
        InputDevice.getDevice(deviceId).apply {

            // Verify that the device has gamepad buttons, control sticks, or both.
            if (sources and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD
                    || sources and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK) {
                // This device is a game controller. Store its device ID.
                gameControllerDeviceIds
                        .takeIf { !it.contains(deviceId) }
                        ?.add(deviceId)
            }
        }
    }
    return gameControllerDeviceIds
}

Java

public ArrayList<Integer> getGameControllerIds() {
    ArrayList<Integer> gameControllerDeviceIds = new ArrayList<Integer>();
    int[] deviceIds = InputDevice.getDeviceIds();
    for (int deviceId : deviceIds) {
        InputDevice dev = InputDevice.getDevice(deviceId);
        int sources = dev.getSources();

        // Verify that the device has gamepad buttons, control sticks, or both.
        if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
                || ((sources & InputDevice.SOURCE_JOYSTICK)
                == InputDevice.SOURCE_JOYSTICK)) {
            // This device is a game controller. Store its device ID.
            if (!gameControllerDeviceIds.contains(deviceId)) {
                gameControllerDeviceIds.add(deviceId);
            }
        }
    }
    return gameControllerDeviceIds;
}

此外,建議您檢查已連結遊戲控制器支援的個別輸入功能。舉例來說,如果您希望遊戲僅使用自身瞭解的實體控制項組合輸入內容,這項功能就很實用。

如要偵測已連結的遊戲控制器是否支援特定按鍵碼或軸碼,請使用下列技巧:

  • 在 Android 4.4 (API 級別 19) 以上版本中,您可以呼叫 hasKeys(int...) 來判斷已連結的遊戲控制器是否支援按鍵碼。
  • 在 Android 3.1 (API 級別 12) 以上版本中,只要呼叫 getMotionRanges(),即可找到已連結遊戲控制器支援的所有軸。然後,在每個傳回的 InputDevice.MotionRange 物件上呼叫 getAxis() 以取得軸 ID。

處理遊戲手把按鈕的按下動作

圖 1 顯示 Android 如何將按鍵代碼和軸值對應至大多數遊戲控制器中的實體控制項。

圖 1 一般遊戲控制器的設定檔。

圖中的摘要參照下列內容:

使用者按下遊戲手把按鈕時產生的常見按鍵碼包括 BUTTON_ABUTTON_BBUTTON_SELECTBUTTON_START。當使用者按下 D-Pad 交叉列中心時,部分遊戲控制器也會觸發 DPAD_CENTER 鍵碼。遊戲可呼叫 getKeyCode()onKeyDown() 等重要事件回呼來檢查按鍵程式碼,如果代表與遊戲相關的事件,則會將其視為遊戲動作進行處理。表 1 列出最常用遊戲手把按鈕的建議遊戲動作。

表 1. 遊戲手把按鈕的建議遊戲動作。

遊戲動作 按鈕按鍵程式碼
透過主選單啟動遊戲,或在遊戲過程中暫停/恢復暫停 BUTTON_START*
顯示選單 BUTTON_SELECT*KEYCODE_MENU*
導覽設計指南中所述的 Android 返回導覽行為相同。 KEYCODE_BACK
返回選單中的上一個項目 BUTTON_B
確認選取項目,或執行主要遊戲動作 BUTTON_ADPAD_CENTER

* 您的遊戲不應依賴「開始」、「選取」或「選單」按鈕。

提示: 建議您在遊戲中提供設定畫面,讓使用者能針對遊戲動作自訂自己的遊戲控制器對應關係。

下列程式碼片段展示如何覆寫 onKeyDown(),以建立 BUTTON_ADPAD_CENTER 按鈕與遊戲動作之間的關聯。

Kotlin

class GameView(...) : View(...) {
    ...

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        var handled = false
        if (event.source and InputDevice.SOURCE_GAMEPAD == InputDevice.SOURCE_GAMEPAD) {
            if (event.repeatCount == 0) {
                when (keyCode) {
                    // Handle gamepad and D-pad button presses to navigate the ship
                    ...

                    else -> {
                        keyCode.takeIf { isFireKey(it) }?.run {
                            // Update the ship object to fire lasers
                            ...
                            handled = true
                        }
                    }
                }
            }
            if (handled) {
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    // Here we treat Button_A and DPAD_CENTER as the primary action
    // keys for the game.
    private fun isFireKey(keyCode: Int): Boolean =
            keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_BUTTON_A
}

Java

public class GameView extends View {
    ...

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean handled = false;
        if ((event.getSource() & InputDevice.SOURCE_GAMEPAD)
                == InputDevice.SOURCE_GAMEPAD) {
            if (event.getRepeatCount() == 0) {
                switch (keyCode) {
                    // Handle gamepad and D-pad button presses to
                    // navigate the ship
                    ...

                    default:
                         if (isFireKey(keyCode)) {
                             // Update the ship object to fire lasers
                             ...
                             handled = true;
                         }
                     break;
                }
            }
            if (handled) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private static boolean isFireKey(int keyCode) {
        // Here we treat Button_A and DPAD_CENTER as the primary action
        // keys for the game.
        return keyCode == KeyEvent.KEYCODE_DPAD_CENTER
                || keyCode == KeyEvent.KEYCODE_BUTTON_A;
    }
}

注意: 在 Android 4.2 (API 級別 17) 以下版本中,系統預設會將 BUTTON_A 視為 Android Back 鍵。如果您的應用程式支援這些 Android 版本,請務必將 BUTTON_A 視為主要遊戲動作。如要判斷裝置目前的 Android SDK 版本,請參閱 Build.VERSION.SDK_INT 值。

處理方向鍵輸入

4 向方向鍵 (D-Pad) 是許多遊戲控制器中常見的實體控制項。Android 會將 D-Pad 的向上和向下鍵事件回報為 AXIS_HAT_Y 事件,範圍介於 -1.0 (向上) 到 1.0 (向下鍵) 到 D-Pad 或向右鍵為 AXIS_HAT_X 事件;範圍介於 -1.0 (左) 到 1.0 (右側)。

有些控制器會回報使用按鍵碼進行 D-Pad 的事件。如果遊戲關注 D-Pad 的按下動作,則應將 hat 軸事件和 D-Pad 按鍵代碼視為相同的輸入事件,如表 2 的建議。

表 2. D-Pad 按鍵代碼和帽子軸值的建議預設遊戲動作。

遊戲動作 D-pad 鍵盤碼 帽子代碼
往上移動 KEYCODE_DPAD_UP AXIS_HAT_Y (適用於值 0 到 -1.0)
往下移動 KEYCODE_DPAD_DOWN AXIS_HAT_Y (適用於值 0 到 1.0)
向左移 KEYCODE_DPAD_LEFT AXIS_HAT_X (適用於值 0 到 -1.0)
向右移 KEYCODE_DPAD_RIGHT AXIS_HAT_X (適用於值 0 到 1.0)

下列程式碼片段顯示了一個輔助類別,可從輸入事件檢查帽子和按鍵程式碼值,以便決定 D-Pad 的方向。

Kotlin

class Dpad {

    private var directionPressed = -1 // initialized to -1

    fun getDirectionPressed(event: InputEvent): Int {
        if (!isDpadDevice(event)) {
            return -1
        }

        // If the input event is a MotionEvent, check its hat axis values.
        (event as? MotionEvent)?.apply {

            // Use the hat axis value to find the D-pad direction
            val xaxis: Float = event.getAxisValue(MotionEvent.AXIS_HAT_X)
            val yaxis: Float = event.getAxisValue(MotionEvent.AXIS_HAT_Y)

            directionPressed = when {
                // Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
                // LEFT and RIGHT direction accordingly.
                xaxis.compareTo(-1.0f) == 0 -> Dpad.LEFT
                xaxis.compareTo(1.0f) == 0 -> Dpad.RIGHT
                // Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
                // UP and DOWN direction accordingly.
                yaxis.compareTo(-1.0f) == 0 -> Dpad.UP
                yaxis.compareTo(1.0f) == 0 -> Dpad.DOWN
                else -> directionPressed
            }
        }
        // If the input event is a KeyEvent, check its key code.
        (event as? KeyEvent)?.apply {

            // Use the key code to find the D-pad direction.
            directionPressed = when(event.keyCode) {
                KeyEvent.KEYCODE_DPAD_LEFT -> Dpad.LEFT
                KeyEvent.KEYCODE_DPAD_RIGHT -> Dpad.RIGHT
                KeyEvent.KEYCODE_DPAD_UP -> Dpad.UP
                KeyEvent.KEYCODE_DPAD_DOWN -> Dpad.DOWN
                KeyEvent.KEYCODE_DPAD_CENTER ->  Dpad.CENTER
                else -> directionPressed
            }
        }
        return directionPressed
    }

    companion object {
        internal const val UP = 0
        internal const val LEFT = 1
        internal const val RIGHT = 2
        internal const val DOWN = 3
        internal const val CENTER = 4

        fun isDpadDevice(event: InputEvent): Boolean =
            // Check that input comes from a device with directional pads.
            event.source and InputDevice.SOURCE_DPAD != InputDevice.SOURCE_DPAD
    }
}

Java

public class Dpad {
    final static int UP       = 0;
    final static int LEFT     = 1;
    final static int RIGHT    = 2;
    final static int DOWN     = 3;
    final static int CENTER   = 4;

    int directionPressed = -1; // initialized to -1

    public int getDirectionPressed(InputEvent event) {
        if (!isDpadDevice(event)) {
           return -1;
        }

        // If the input event is a MotionEvent, check its hat axis values.
        if (event instanceof MotionEvent) {

            // Use the hat axis value to find the D-pad direction
            MotionEvent motionEvent = (MotionEvent) event;
            float xaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_X);
            float yaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_Y);

            // Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
            // LEFT and RIGHT direction accordingly.
            if (Float.compare(xaxis, -1.0f) == 0) {
                directionPressed =  Dpad.LEFT;
            } else if (Float.compare(xaxis, 1.0f) == 0) {
                directionPressed =  Dpad.RIGHT;
            }
            // Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
            // UP and DOWN direction accordingly.
            else if (Float.compare(yaxis, -1.0f) == 0) {
                directionPressed =  Dpad.UP;
            } else if (Float.compare(yaxis, 1.0f) == 0) {
                directionPressed =  Dpad.DOWN;
            }
        }

        // If the input event is a KeyEvent, check its key code.
        else if (event instanceof KeyEvent) {

           // Use the key code to find the D-pad direction.
            KeyEvent keyEvent = (KeyEvent) event;
            if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
                directionPressed = Dpad.LEFT;
            } else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
                directionPressed = Dpad.RIGHT;
            } else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
                directionPressed = Dpad.UP;
            } else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
                directionPressed = Dpad.DOWN;
            } else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
                directionPressed = Dpad.CENTER;
            }
        }
        return directionPressed;
    }

    public static boolean isDpadDevice(InputEvent event) {
        // Check that input comes from a device with directional pads.
        if ((event.getSource() & InputDevice.SOURCE_DPAD)
             != InputDevice.SOURCE_DPAD) {
             return true;
         } else {
             return false;
         }
     }
}

您可以在遊戲中任何要處理 D-Pad 輸入的位置 (例如 onGenericMotionEvent()onKeyDown() 回呼) 使用這個輔助類別。

例如:

Kotlin

private val dpad = Dpad()
...
override fun onGenericMotionEvent(event: MotionEvent): Boolean {
    if (Dpad.isDpadDevice(event)) {
        when (dpad.getDirectionPressed(event)) {
            Dpad.LEFT -> {
                // Do something for LEFT direction press
                ...
                return true
            }
            Dpad.RIGHT -> {
                // Do something for RIGHT direction press
                ...
                return true
            }
            Dpad.UP -> {
                // Do something for UP direction press
                ...
                return true
            }
            ...
        }
    }

    // Check if this event is from a joystick movement and process accordingly.
    ...
}

Java

Dpad dpad = new Dpad();
...
@Override
public boolean onGenericMotionEvent(MotionEvent event) {

    // Check if this event if from a D-pad and process accordingly.
    if (Dpad.isDpadDevice(event)) {

       int press = dpad.getDirectionPressed(event);
       switch (press) {
            case LEFT:
                // Do something for LEFT direction press
                ...
                return true;
            case RIGHT:
                // Do something for RIGHT direction press
                ...
                return true;
            case UP:
                // Do something for UP direction press
                ...
                return true;
            ...
        }
    }

    // Check if this event is from a joystick movement and process accordingly.
    ...
}

處理搖桿移動

玩家在遊戲控制器上移動搖桿時,Android 會回報 MotionEvent,其中包含 ACTION_MOVE 動作程式碼,以及搖桿軸的更新位置。遊戲可以使用 MotionEvent 提供的資料,判斷是否有所關注的搖桿動作。

請注意,搖桿動作事件可能會在單一物件中將多個動作樣本一次批次處理。MotionEvent 物件包含每個搖桿的目前位置,以及每個軸的多個歷史位置。使用動作代碼 ACTION_MOVE 回報動作事件時 (例如搖桿動作),Android 會批次處理軸值以提高效率。軸的歷史值包含比目前軸值之前的一組不同值,以及比任何先前動作事件回報的值還要近期。詳情請參閱 MotionEvent 參考資料。

您可以使用歷史資訊,根據搖桿輸入更準確地轉譯遊戲物件的動作。如要擷取目前和歷史值,請呼叫 getAxisValue()getHistoricalAxisValue()。您也可以呼叫 getHistorySize(),在搖桿事件中找到歷史點數量。

下列程式碼片段顯示如何覆寫 onGenericMotionEvent() 回呼以處理搖桿輸入內容。首先,您應處理軸的歷史值,然後處理其目前位置。

Kotlin

class GameView(...) : View(...) {

    override fun onGenericMotionEvent(event: MotionEvent): Boolean {

        // Check that the event came from a game controller
        return if (event.source and InputDevice.SOURCE_JOYSTICK == InputDevice.SOURCE_JOYSTICK
                && event.action == MotionEvent.ACTION_MOVE) {

            // Process the movements starting from the
            // earliest historical position in the batch
            (0 until event.historySize).forEach { i ->
                // Process the event at historical position i
                processJoystickInput(event, i)
            }

            // Process the current movement sample in the batch (position -1)
            processJoystickInput(event, -1)
            true
        } else {
            super.onGenericMotionEvent(event)
        }
    }
}

Java

public class GameView extends View {

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {

        // Check that the event came from a game controller
        if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) ==
                InputDevice.SOURCE_JOYSTICK &&
                event.getAction() == MotionEvent.ACTION_MOVE) {

            // Process all historical movement samples in the batch
            final int historySize = event.getHistorySize();

            // Process the movements starting from the
            // earliest historical position in the batch
            for (int i = 0; i < historySize; i++) {
                // Process the event at historical position i
                processJoystickInput(event, i);
            }

            // Process the current movement sample in the batch (position -1)
            processJoystickInput(event, -1);
            return true;
        }
        return super.onGenericMotionEvent(event);
    }
}

使用搖桿輸入之前,您必須先判斷搖桿是否置中,然後據此計算軸移動。搖桿通常設有「平坦」區域,也就是靠近 (0,0) 座標附近的值範圍,系統會將該座標視為中心點。如果 Android 回報的軸值落在平坦區域內,就應將控制器保持不動 (也就是讓控制器保持不動)。

以下程式碼片段是計算每個軸線移動的輔助方法。您可以在下方所述的 processJoystickInput() 方法中叫用這個輔助程式。

Kotlin

private fun getCenteredAxis(
        event: MotionEvent,
        device: InputDevice,
        axis: Int,
        historyPos: Int
): Float {
    val range: InputDevice.MotionRange? = device.getMotionRange(axis, event.source)

    // A joystick at rest does not always report an absolute position of
    // (0,0). Use the getFlat() method to determine the range of values
    // bounding the joystick axis center.
    range?.apply {
        val value: Float = if (historyPos < 0) {
            event.getAxisValue(axis)
        } else {
            event.getHistoricalAxisValue(axis, historyPos)
        }

        // Ignore axis values that are within the 'flat' region of the
        // joystick axis center.
        if (Math.abs(value) > flat) {
            return value
        }
    }
    return 0f
}

Java

private static float getCenteredAxis(MotionEvent event,
        InputDevice device, int axis, int historyPos) {
    final InputDevice.MotionRange range =
            device.getMotionRange(axis, event.getSource());

    // A joystick at rest does not always report an absolute position of
    // (0,0). Use the getFlat() method to determine the range of values
    // bounding the joystick axis center.
    if (range != null) {
        final float flat = range.getFlat();
        final float value =
                historyPos < 0 ? event.getAxisValue(axis):
                event.getHistoricalAxisValue(axis, historyPos);

        // Ignore axis values that are within the 'flat' region of the
        // joystick axis center.
        if (Math.abs(value) > flat) {
            return value;
        }
    }
    return 0;
}

總結來說,以下是在遊戲中處理搖桿動作的方法:

Kotlin

private fun processJoystickInput(event: MotionEvent, historyPos: Int) {

    val inputDevice = event.device

    // Calculate the horizontal distance to move by
    // using the input value from one of these physical controls:
    // the left control stick, hat axis, or the right control stick.
    var x: Float = getCenteredAxis(event, inputDevice, MotionEvent.AXIS_X, historyPos)
    if (x == 0f) {
        x = getCenteredAxis(event, inputDevice, MotionEvent.AXIS_HAT_X, historyPos)
    }
    if (x == 0f) {
        x = getCenteredAxis(event, inputDevice, MotionEvent.AXIS_Z, historyPos)
    }

    // Calculate the vertical distance to move by
    // using the input value from one of these physical controls:
    // the left control stick, hat switch, or the right control stick.
    var y: Float = getCenteredAxis(event, inputDevice, MotionEvent.AXIS_Y, historyPos)
    if (y == 0f) {
        y = getCenteredAxis(event, inputDevice, MotionEvent.AXIS_HAT_Y, historyPos)
    }
    if (y == 0f) {
        y = getCenteredAxis(event, inputDevice, MotionEvent.AXIS_RZ, historyPos)
    }

    // Update the ship object based on the new x and y values
}

Java

private void processJoystickInput(MotionEvent event,
        int historyPos) {

    InputDevice inputDevice = event.getDevice();

    // Calculate the horizontal distance to move by
    // using the input value from one of these physical controls:
    // the left control stick, hat axis, or the right control stick.
    float x = getCenteredAxis(event, inputDevice,
            MotionEvent.AXIS_X, historyPos);
    if (x == 0) {
        x = getCenteredAxis(event, inputDevice,
                MotionEvent.AXIS_HAT_X, historyPos);
    }
    if (x == 0) {
        x = getCenteredAxis(event, inputDevice,
                MotionEvent.AXIS_Z, historyPos);
    }

    // Calculate the vertical distance to move by
    // using the input value from one of these physical controls:
    // the left control stick, hat switch, or the right control stick.
    float y = getCenteredAxis(event, inputDevice,
            MotionEvent.AXIS_Y, historyPos);
    if (y == 0) {
        y = getCenteredAxis(event, inputDevice,
                MotionEvent.AXIS_HAT_Y, historyPos);
    }
    if (y == 0) {
        y = getCenteredAxis(event, inputDevice,
                MotionEvent.AXIS_RZ, historyPos);
    }

    // Update the ship object based on the new x and y values
}

如果遊戲控制器包含單一搖桿以外的更精密功能,請遵循下列最佳做法:

  • 處理雙控制器滴答。許多遊戲控制器都提供左右搖桿。對於左滴答,Android 會將水平動作回報為 AXIS_X 事件,並將垂直動作回報為 AXIS_Y 事件。以右側滴答式來說,Android 會以 AXIS_Z 事件和垂直動作的形式,將水平動作回報為 AXIS_RZ 事件。請務必處理程式碼中的兩個控制器滴答。
  • 處理額外觸發動作的觸發動作 (但提供替代輸入法)。部分控制器具有左右肩觸發條件。如果有這些觸發事件,Android 會將左側觸發事件回報為 AXIS_LTRIGGER 事件,並將右側觸發事件回報為 AXIS_RTRIGGER 事件。在 Android 4.3 (API 級別 18) 中,產生 AXIS_LTRIGGER 的控制器也會回報 AXIS_BRAKE 軸的相同值。AXIS_RTRIGGERAXIS_GAS 也是如此。Android 會以正規化值 0.0 (發布) 到 1.0 (完全按下) 的正規化值回報所有類比觸發事件壓力。並非所有控制器都有觸發條件,因此請考慮允許玩家使用其他按鈕執行這些遊戲動作。