检测用户输入

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

将用户输入连接到行动可以让用户更好地控制体验功能。在本教程中,您将绑定重新加载操作到特定键。

开始

本教程使用在 创建玩家工具 中创建的 爆破者 工具。您可以遵循这些说明创建工具,或者下载 冲击波 模型并插入 启动包

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

  1. 在浏览器中,打开 模型 页面,单击 获取 按钮。这将将模型添加到您的道具中。
  2. 在工作室中,前往 视图 选项卡,然后单击 工具箱
  3. 在工具箱窗口中,单击 库存 按钮。然后,确保下拉菜单位于 我的模型
  4. 选择 冲击波 模型将其添加到体验中。

创建一个行动处理器

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

  1. 在发射器内打开 工具控制器 LocalScript

  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 以检测并与动作动关联。

键码是代表不同输入按钮的值,例如键盘按钮或控制器按钮。完整的代码列表可用 here .

  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. 创建一个新函数 called 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 函数,然后在重新加载动画完成后重新激活它。