我是一名新的 roblox 程序员和开发人员,我正在尝试制作我的第一个真正的游戏,这是一个 obby,但每隔 10 秒你的控件就会随机变化。因此,当您按 w 向前走时,您会向右走,并且您可以在屏幕顶部有一个 GUI,显示距离控件更改还有多长时间以及当前控件是什么。但因为我是一名新开发人员,所以我很难制作一个更改控件的脚本。
我尝试制作脚本:
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local function RandomizeControls(player)
local controls = {
["w"] = "s", ["a"] = "d", ["s"] = "w", ["d"] = "a",
["up"] = "down", ["down"] = "up", ["left"] = "right", ["right"] = "left",
["q"] = "e", ["e"] = "q", ["z"] = "x", ["x"] = "z"
}
local function ChangeControl(input)
return controls[input] or input
end
local function OnInputBegan(input, gameProcessedEvent)
if not gameProcessedEvent then
local newKeyCode = ChangeControl(input.KeyCode.Name)
UserInputService.InputBegan:Fire({ KeyCode = Enum.KeyCode[newKeyCode] })
end
end
UserInputService.InputBegan:Connect(OnInputBegan)
while true do
wait(10)
controls = {
["w"] = "s", ["a"] = "d", ["s"] = "w", ["d"] = "a",
["up"] = "down", ["down"] = "up", ["left"] = "right", ["right"] = "left",
["q"] = "e", ["e"] = "q", ["z"] = "x", ["x"] = "z"
}
end
end
Players.PlayerAdded:Connect(function(player)
RandomizeControls(player)
end)
您当前的方法不起作用的原因(还)是因为您没有更改控制器模块的代码来适应您的控件。
最好的方法是克隆 PlayerModule 及其子项,可以在运行时在 Player > Player... > PlayerScripts 中找到,并删除负责处理移动的 userinputservice 事件侦听器,然后您可以修改脚本以要求控制器模块并直接触发运动功能。
以下是键盘模块内的代码片段(负责使用键盘控件进行移动)。我调整了原始代码以反转控件,例如按 A 向右走,而不是向左走等等。
ContextActionService:BindActionAtPriority("moveForwardAction", handleMoveForward, false,
self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterBackward)
ContextActionService:BindActionAtPriority("moveBackwardAction", handleMoveBackward, false,
self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterForward)
ContextActionService:BindActionAtPriority("moveLeftAction", handleMoveLeft, false,
self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterRight)
ContextActionService:BindActionAtPriority("moveRightAction", handleMoveRight, false,
self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterLeft)
ContextActionService:BindActionAtPriority("jumpAction", handleJumpAction, false,
self.CONTROL_ACTION_PRIORITY, Enum.PlayerActions.CharacterJump)
要使您的自定义模块生效,请将其放置在 StarterPlayer > StarterPlayerScripts 中(见图)