Roblox ContextAction两次触发问题

我尝试使用RobloxContextActionService在Roblox移动设备上创建一个按钮,但每当我按下GUI按钮时,它就会触发,而当我释放它时,它也会触发,我希望在我按下按钮时只触发一次。

function tpggle()
    print("L pressed")
end
ContextActionService:BindAction("ToggleLight", tpggle, true, Enum.KeyCode.L)
ContextActionService:SetTitle("ToggleLight","L")
ContextActionService:SetPosition("ToggleLight",UDim2.new(1, -97,1, -133))
我期望的结果是:
  L pressed
我得到的结果是:
  L pressed(x2)
点赞
用户2858170
用户2858170

你的 tqggle 动作处理程序很可能被调用了两次。一次是按下按钮,一次是释放按钮。

试试这个:

function tpggle(actionName, inputState, inputObj)
  if inputState == Enum.UserInputState.Begin then
    print("L pressed")
  end
end

这样只会在按下按钮时打印出一些内容。

参考:

https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction

https://developer.roblox.com/en-us/api-reference/property/InputObject/UserInputState

2019-08-12 12:18:27