中级教程

检测用户输入

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

将用户输入连接到动作使用户能够更加直观和更好地控制您体验的功能。在本教程中,您将把一个重载动作绑定到一个特定的按键。

开始

本教程使用在 创建玩家工具 中创建的 Blaster 工具。您可以按照这些说明创建工具,或者下载 Blaster 模型并将其插入 StarterPack

模型可以添加到您的库存中,以便在任何体验中使用。要将模型添加到您的体验中:

  1. 在浏览器中打开 模型 页面,点击 获取 按钮。这将把模型添加到您的库存中。
  2. 从 Studio 的 窗口 菜单或 主页 选项卡工具栏中,打开 工具箱 并选择 库存 选项卡。
  3. 确保下拉框设置为 我的模型
  4. 选择 Blaster 模型将其添加到体验中。

创建动作处理程序

首先,您需要一个函数来处理检测到的用户输入。

  1. 打开 Blaster 中的 ToolController LocalScript

    选择了 Blaster 工具内的 ToolController 脚本的资源管理器视图

  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 是我们在美国及其他国家或地区的注册与未注册商标。