偵測使用者輸入

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

將使用者輸入連接到動作可以讓使用者對您遊戲的功能有更好且更直觀的控制。在本教程中,您將把重新裝填的動作綁定到特定的按鍵。

開始

本教程使用在 創建玩家工具 中創建的 Blaster 工具。您可以按照那些指示創建工具,或者下載 Blaster 模型並將其插入 StarterPack

模型可以添加到您的庫存中,以便在任何遊戲之間使用。要將模型添加到您的遊戲中:

  1. 在瀏覽器中,打開 模型 頁面,點擊 獲取 按鈕。這會將模型添加到您的庫存中。
  2. 從 Studio 的 視窗 菜單或 首頁 標籤工具欄中,打開 工具箱 並選擇 庫存 標籤。
  3. 確保下拉選單設置為 我的模型
  4. 選擇 Blaster 模型以將其添加到遊戲中。

創建動作處理器

首先,您需要一個函數來處理當偵測到使用者輸入時的情況。

  1. 打開 Blaster 內的 ToolController LocalScript

    選擇 Blaster 工具內的 ToolController 腳本的 Explorer 視圖

  2. 創建一個變數來存儲動作的名稱。

    local tool = script.Parent
    local RELOAD_ACTION = "reloadWeapon"
    local function toolEquipped()
    tool.Handle.Equip:Play()
    end
    local function toolActivated()
    tool.Handle.Activate:Play()
    end
    tool.Equipped:Connect(toolEquipped)
    tool.Activated:Connect(toolActivated)
  3. 創建一個名為 onAction 的函數,接收三個參數:actionNameinputStateinputObject。這將是當偵測到使用者輸入時運行的函數。

    local tool = script.Parent
    local RELOAD_ACTION = "reloadWeapon"
    local function onAction(actionName, inputState, inputObject)
    end
    local function toolEquipped()
    tool.Handle.Equip:Play()
    end
  4. 在函數內,檢查給定的 actionName 是否與重新裝填動作名稱匹配,並確保 inputStateUserInputState.Begin(開始狀態)。這很重要,因為該函數會在每次 inputState 變更時運行,但重新裝填只需要發生一次。

    local function onAction(actionName, inputState, inputObject)
    if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin then
    end
    end
  5. 為了讓使用者重新裝填時更明顯,將工具的 TextureId 短暫更改為 "rbxassetid://6593020923",然後再改回其原始值 "rbxassetid://92628145"

    local function onAction(actionName, inputState, inputObject)
    if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin then
    tool.TextureId = "rbxassetid://6593020923"
    task.wait(2)
    tool.TextureId = "rbxassetid://92628145"
    end
    end

綁定動作

ContextActionService 可以用來 綁定 一個函數到特定的輸入,使用 BindAction 函數,該函數接受幾個參數:

  • 動作的名稱。
  • 處理該動作的函數(也稱為 "回調")。
  • 是否應顯示觸控螢幕按鈕。
  • 任何數量的 Enum.KeyCodes 來偵測並與該動作關聯。

KeyCodes 是表示不同輸入按鈕的值,例如鍵盤按鍵或控制器按鈕。完整的代碼列表可以在 這裡 找到。

  1. 在腳本的頂部獲取 ContextActionService

    local ContextActionService = game:GetService("ContextActionService")
    local tool = script.Parent
    local RELOAD_ACTION = "reloadWeapon"
  2. toolEquipped 函數內,調用 BindAction 並傳遞以下參數:

    • 動作的名稱(RELOAD_ACTION
    • 動作處理器(onAction
    • 創建觸控按鈕的值(true
    • 要偵測的按鍵(Enum.KeyCode.R
    local RELOAD_ACTION = "reloadWeapon"
    local function onAction(actionName, inputState, inputObject)
    if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin then
    tool.TextureId = "rbxassetid://6593020923"
    task.wait(2)
    tool.TextureId = "rbxassetid://92628145"
    end
    end
    local function toolEquipped()
    ContextActionService:BindAction(RELOAD_ACTION, onAction, true, Enum.KeyCode.R)
    tool.Handle.Equip:Play()
    end
  3. 進行遊戲測試,裝備工具並按下鍵盤上的 R 鍵。背包圖示應該短暫變更為等待符號,以表示武器正在重新裝填:

解除綁定動作

當使用者卸下工具時,動作需要被 解除綁定,以便他們無法在未裝備工具的情況下重新裝填。

  1. 創建一個名為 toolUnequipped 的新函數,並調用 UnbindAction,傳遞動作名稱。

    local function toolEquipped()
    ContextActionService:BindAction(RELOAD_ACTION, onAction, true, Enum.KeyCode.R)
    tool.Handle.Equip:Play()
    end
    local function toolUnequipped()
    ContextActionService:UnbindAction(RELOAD_ACTION)
    end
    local function toolActivated()
    tool.Handle.Activate:Play()
    end
    tool.Equipped:Connect(toolEquipped)
    tool.Activated:Connect(toolActivated)
  2. toolUnequipped 函數連接到 Unequipped 事件,以便當事件觸發時運行該函數。

    local ContextActionService = game:GetService("ContextActionService")
    local tool = script.Parent
    local RELOAD_ACTION = "reloadWeapon"
    local function onAction(actionName, inputState, inputObject)
    if actionName == RELOAD_ACTION and inputState == Enum.UserInputState.Begin then
    tool.TextureId = "rbxassetid://6593020923"
    task.wait(2)
    tool.TextureId = "rbxassetid://92628145"
    end
    end
    local function toolEquipped()
    ContextActionService:BindAction(RELOAD_ACTION, onAction, true, Enum.KeyCode.R)
    tool.Handle.Equip:Play()
    end
    local function toolUnequipped()
    ContextActionService:UnbindAction(RELOAD_ACTION)
    end
    local function toolActivated()
    tool.Handle.Activate:Play()
    end
    tool.Equipped:Connect(toolEquipped)
    tool.Unequipped:Connect(toolUnequipped)
    tool.Activated:Connect(toolActivated)
  3. 進行遊戲測試以確認一切正常。您應該能在工具裝備時重新裝填,但在卸下時無法重新裝填。

您的重新裝填動畫現在已完成 - 為了增加挑戰,嘗試在每次發射時計算彈藥計數器。然後,當槍沒有彈藥時,您可以停用 toolActivated 函數,然後在重新裝填動畫完成後重新啟用它。

©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。