XRInputSourceArray: forEach() 方法
XRInputSourceArray 方法 forEach() 會對陣列中的每個輸入源執行一次指定的 callback 函式,從索引 0 開始,直到列表末尾。
語法
js
forEach(callback)
forEach(callback, thisArg)
引數
回撥-
一個函式,將為
xrInputSourceArray陣列中的每個條目執行一次。該 callback 最多接受三個引數:currentValue-
一個
XRInputSource物件,這是當前正在處理的xrInputSourceArray中的項的值。 currentIndex可選-
一個整數值,表示
currentValue元素在陣列中的索引。如果你不需要知道索引號,可以省略此引數。 sourceList可選-
正在處理的
XRInputSourceArray物件。如果你不需要此資訊,可以省略此引數。
thisArg可選-
執行 callback 時要用於
this的值。請注意,如果你使用 箭頭函式表示法(=>)來提供 callback,則可以省略thisArg,因為所有箭頭函式都將this繫結到詞法作用域。
返回值
Undefined。
示例
此示例程式碼段獲取會話的輸入列表,並嘗試使用它來處理它支援的每種型別的輸入裝置。
js
let inputSources = xrSession.inputSources;
inputSources.forEach((input) => {
if (input.gamepad) {
checkGamepad(input.gamepad);
} else if (
input.targetRayMode === "tracked-pointer" &&
input.handedness === player.handedness
) {
/* Handle main hand controller */
handleMainHandInput(input);
} else {
/* Handle other inputs */
}
});
對於列表中的每個輸入,callback 將將手柄輸入分派給 checkGamepad(),並將輸入的 Gamepad 物件(從其 gamepad 屬性獲取)作為輸入。
對於其他裝置,我們會查詢玩家主手上的 tracked-pointer 裝置,並將它們分派給 handleMainHandInput() 方法。
規範
此特性似乎未在任何規範中定義。瀏覽器相容性
載入中…
另見
- 輸入和輸入源
Array方法forEach()XRInputSource