<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                > 編寫:[heray1990](https://github.com/heray1990) - 原文:[http://developer.android.com/training/game-controllers/controller-input.html](http://developer.android.com/training/game-controllers/controller-input.html) 在系統層面上,Android會以Android鍵值和坐標值的形式來報告來自游戲控制器的輸入事件。在我們的游戲應用里,我們可以接收這些鍵值和坐標值,并將它們轉化成特定的游戲行為。 當玩家將一個游戲控制器通過有線連接或者無線配對到基于Android的設備時,系統會自動檢測控制器,將它設置成輸入設備并且開始報告它的輸入事件。我們的游戲應用可以通過在活動的[Activity](# "An activity represents a single screen with a user interface.")或者聚焦的[View](http://developer.android.com/reference/android/view/View.html)里調用下面這些回調方法來接收上述輸入事件(要么在[Activity](# "An activity represents a single screen with a user interface."),要么在[View](http://developer.android.com/reference/android/view/View.html)中實現實現這些回調方法,不要兩個地方都實現回調)。 - From [Activity](# "An activity represents a single screen with a user interface.") - [dispatchGenericMotionEvent(android.view. MotionEvent)](http://developer.android.com/reference/android/app/Activity.html#dispatchGenericMotionEvent(android.view.MotionEvent)) - 處理一般的運動事件,如搖動搖桿 - [dispatchKeyEvent(android.view.KeyEvent)](http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent(android.view.KeyEvent)) - 處理按鍵事件,如按下或者釋放游戲鍵盤的按鍵或者十字方向鍵。 - From [View](http://developer.android.com/reference/android/view/View.html) - [onGenericMotionEvent(android.view.MotionEvent)](http://developer.android.com/reference/android/view/View.html#onGenericMotionEvent(android.view.MotionEvent)) - 處理一般的運動事件,如搖動搖桿 - [onKeyDown(int, android.view.KeyEvent)](http://developer.android.com/reference/android/view/View.html#onKeyDown(int, android.view.KeyEvent)) - 處理按下一個按鍵的事件,如按下游戲鍵盤的按鍵或者十字方向鍵。 - [onKeyUp(int, android.view.KeyEvent)](http://developer.android.com/reference/android/view/View.html#onKeyUp(int, android.view.KeyEvent)) - 處理釋放一個按鍵的事件,如釋放游戲鍵盤的按鍵或者十字方向鍵。 建議的方法是從與用戶交互的[View](http://developer.android.com/reference/android/view/View.html)對象捕獲事件。請查看下面回調提供的對象,從而獲取關于接收到的輸入事件的類型: [KeyEvent](http://developer.android.com/reference/android/view/KeyEvent.html):描述方向按鍵和游戲按鍵事件的對象。按鍵事件伴隨著一個表示特定按鍵觸發的_按鍵碼值(key code)_,如[DPAD_DOWN](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_DPAD_DOWN)或者[BUTTON_A](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_A)。我們可以通過調用[getKeyCode()](http://developer.android.com/reference/android/view/KeyEvent.html#getKeyCode())或者從按鍵事件回調方法(如[onKeyDown()](http://developer.android.com/reference/android/view/View.html#onKeyDown(int, android.view.KeyEvent)))來獲得按鍵碼值。 [MotionEvent](http://developer.android.com/reference/android/view/MotionEvent.html):描述搖桿和肩鍵運動的輸入。動作事件伴隨著一個動作碼(action code)和一系列_坐標值(axis values)_。動作碼表示出現變化的狀態,例如搖動一個搖桿。坐標值描述了位置和其它運動屬性,例如[AXIS_X](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_X)或者[AXIS_RTRIGGER](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_RTRIGGER)。我們可以通過調用[getAction()](http://developer.android.com/reference/android/view/MotionEvent.html#getAction())來獲得動作碼,通過調用[getAxisValue()](http://developer.android.com/reference/android/view/MotionEvent.html#getAxisValue(int))來獲得坐標值。 這節課主要介紹如何通過上述的[View](http://developer.android.com/reference/android/view/View.html)回調方法和處理[KeyEvent](http://developer.android.com/reference/android/view/KeyEvent.html)和[MotionEvent](http://developer.android.com/reference/android/view/MotionEvent.html)對象來處理常用控制器(游戲鍵盤按鍵、方向按鍵和搖桿)的輸入。 ### 驗證游戲控制器是否已連接 在報告輸入事件的時候,Android不會區分游戲控制器事件與非游戲控制器事件。例如,一個觸屏動作會產生一個表示觸摸表面上X坐標的[AXIS_X](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_X),但是一個搖桿動作產生的[AXIS_X](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_X)則表示搖桿水平移動的位置。如果我們的游戲關注游戲控制器的輸入,那么我們應該首先檢測事件相應的來源類型。 通過調用[getSources()](http://developer.android.com/reference/android/view/InputDevice.html#getSources())來獲得設備上支持的輸入類型的位字段,來驗證一個已連接的輸入設備是一個游戲控制器。我們可以測試以查看下面的字段是否被設置: - [SOURCE_GAMEPAD](http://developer.android.com/reference/android/view/InputDevice.html#SOURCE_GAMEPAD)源類型表示輸入設備有游戲手柄按鍵(如,[BUTTON_A](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_A))。注意隨然一般的游戲手柄都會有方向控制鍵,但是這個源類型并不代表游戲控制器具有十字方向鍵。 - [SOURCE_DPAD](http://developer.android.com/reference/android/view/InputDevice.html#SOURCE_DPAD)源類型表示輸入設備有十字方向鍵(如,[DPAD_UP](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_DPAD_UP))。 - [SOURCE_JOYSTICK](http://developer.android.com/reference/android/view/InputDevice.html#SOURCE_JOYSTICK)源類型表示輸入設備有遙控桿(如,會用[AXIS_X](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_X)和[AXIS_Y](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_Y)記錄動作的搖桿)。 下面的一小段代碼介紹了一個helper方法,它的作用是讓你檢驗已接入的輸入設備是否是游戲控制器。如果檢測到是游戲控制器,那么這個方法會獲取到游戲控制器的設備ID。然后,我們應該將每個設備ID與游戲中的玩家關聯起來,并且單獨處理每個已接入的玩家的游戲操作。想更詳細地了解關于在一臺Android設備中同時支持多個游戲控制器的方法,請見[支持多個游戲控制器](#)。 ~~~ public ArrayList getGameControllerIds() { ArrayList gameControllerDeviceIds = new ArrayList(); 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 level 19)或者更高的系統中,調用[hasKeys(int)](http://developer.android.com/reference/android/view/InputDevice.html#hasKeys(int...))來確定游戲控制器是否支持一個按鍵碼。 - 在Android 3.1(API level 12)或者更高的系統中,首先調用[getMotionRanges()](http://developer.android.com/reference/android/view/InputDevice.html#getMotionRanges()),然后在每個返回的[InputDevice.MotionRange](http://developer.android.com/reference/android/view/InputDevice.MotionRange.html)對象中調用[getAxis()](http://developer.android.com/reference/android/view/InputDevice.MotionRange.html#getAxis())來獲得坐標ID。這樣就可以得到游戲控制器支持的所有可用坐標軸。 ### 處理游戲手柄按鍵 Figure 1介紹了Android如何將按鍵碼和坐標值映射到實際的游戲手柄上。 ![game-controller-profiles](https://box.kancloud.cn/2015-07-28_55b7247a73d8f.png "Figure 1. Profile for a generic game controller.") **Figure 1.** 一個常用的游戲手柄的外形 上圖的標注對應下面的內容: 1. [AXIS_HAT_X](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_HAT_X), [AXIS_HAT_Y](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_HAT_Y), [DPAD_UP](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_DPAD_UP), [DPAD_DOWN](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_DPAD_DOWN), [DPAD_LEFT](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_DPAD_LEFT), [DPAD_RIGHT](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_DPAD_RIGHT) 1. [AXIS_X](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_X), [AXIS_Y](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_Y), [BUTTON_THUMBL](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_THUMBL) 1. [AXIS_Z](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_Z), [AXIS_RZ](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_RZ), [BUTTON_THUMBR](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_THUMBR) 1. [BUTTON_X](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_X) 1. [BUTTON_A](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_A) 1. [BUTTON_Y](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_Y) 1. [BUTTON_B](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_B) 1. [BUTTON_R1](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_R1) 1. [AXIS_RTRIGGER](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_RTRIGGER), [AXIS_THROTTLE](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_THROTTLE) 1. [AXIS_LTRIGGER](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_LTRIGGER), [AXIS_BRAKE](http://developer.android.com/reference/android/view/MotionEvent.html#AXIS_BRAKE) 1. [BUTTON_L1](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_L1) 游戲手柄產生的通用的按鍵碼包括[BUTTON_A](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_A)、[BUTTON_B](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_B)、[BUTTON_SELECT](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_SELECT)和[BUTTON_START](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_START)。當按下十字方向鍵的中間交叉按鍵時,一些游戲控制器會觸發[DPAD_CENTER](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_DPAD_CENTER)按鍵碼。我們的游戲可以通過調用[getKeyCode()](http://developer.android.com/reference/android/view/KeyEvent.html#getKeyCode())或者從按鍵事件回調(如[onKeyDown()](http://developer.android.com/reference/android/view/View.html#onKeyDown(int, android.view.KeyEvent)))得到按鍵碼。如果一個事件與我們的游戲相關,那么將其處理成一個游戲動作。Table 1列出供大多數通用游戲手柄的按鈕使用的推薦的游戲動作。 **Table 1.** 供游戲手柄使用的推薦的游戲動作 | 游戲動作 | 按鍵碼 | |-----|-----| | 在主菜單中啟動游戲,或者在游戲過程中暫停/取消暫停 | [BUTTON_START](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_START)* | | 顯示菜單 | [BUTTON_SELECT](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_SELECT)* 和 [KEYCODE_MENU](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_MENU)* | | 跟Android導航設計指導中的Back導航行為一樣 | [KEYCODE_BACK](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BACK) | | 返回到菜單中上一項 | [BUTTON_B](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_B) | | 確認選擇,或者執行主要的游戲動作 | [BUTTON_A](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_BUTTON_A) 和 [DPAD_CENTER](http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_DPAD_CENTER) | * _我們的游戲不應該依賴于Start、Select或者Menu按鍵的存在。_ > **Tip:**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看