这个项目是另一个使用循环的实用示例。对于这个桥,玩家将触摸一个按钮,在它消失之前将桥变为可行走的时间有限。要创建计时器并向玩家显示剩余时间,你会使用循环。
设置项目
这座桥可以包含在任何以运动为基础的挑战项目中,例如障碍赛。
创建零件
找到一个地方来建造桥,例如河流或障碍赛中的大间隙。创建三个 锚定 部分,如下所示。
- 计时显示
- 桥
- 按钮桥
当不活跃时,桥会半透明。要实现此效果,请选择 桥 并更改其属性。
- 透明度 = 0.8
- 可以碰撞 = 否
创建计时器显示
当穿过桥时,玩家需要看到桥消失前还有多少秒。显示图像或文本的一个方法是将一个名为 Surface GUI 的对象添加到零件上。 表面图形用户界面 也可用于创建游戏内标志、自定义生命条和库存系统。本教程会快速通过这一点,但更多信息可以在 教程 部分中找到。
选择计时显示并添加一个 SurfaceGui 。然后,父辈到 Surface Gui 添加一个 文本标签 。
选择面板 GUI。在属性中,进行以下更改:
- 更改 脸部 以便您可以看到玩家正在查看计时器前方的文本标签。
选择文本标签。在属性中,进行以下更改:
- 将 大小 设置为{1, 0},{1, 0}。
- 将 文本缩放 设置为真。
- 将 文本 设置为空白。文本将使用脚本进行更新。
设置脚本
现在计时器已经到场景,创建一个脚本来控制桥并向玩家显示倒计时数字。
在桥部分中,添加一个名为 TimedBridge 的新脚本。在该脚本中,创建以下变量。
local bridge = script.Parentlocal button = workspace.ButtonBridgelocal timerText = workspace.TimerDisplay.SurfaceGui.TextLabel-- 桥梁能保持多久的稳定local timerDuration = 5
编写触摸互动代码
要使用桥,你需要创建两个函数。一个函数将使桥可行走并显示计时器。另一个函数会听到,如果玩家触碰激活桥的按钮。
创建一个名为 startTimer() 的新函数,内部包含打印声明。你将使用打印声明来测试你的验证码。
local timerDuration = 5local function startTimer()print("Countdown started")end编写一个名为 buttonPressed() 的函数来检查是否有人形触碰按钮。函数应接受参数 partTouched ,然后在那部分检测到人形时有一个空 if 声明。
local function startTimer()print("Countdown started")endlocal function buttonPressed(partTouched)local character = partTouched.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenendend要启动计时器,在 if 声明中,调用 startTimer() 函数。
local function buttonPressed(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenstartTimer()endend在 buttonPressed() 结束之下,连接 buttonPressed() 函数到按钮的 Touched 事件。
local function buttonPressed(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenstartTimer()endendbutton.Touched:Connect(buttonPressed)运行项目。触摸零件并检查输出窗口以查看打印声明。
排除问题的提示
在这一点上,如果桥不像预期的那样工作,请尝试以下方法之一。 问题 : 错误消息说: "... is not a valid member of workspace" .
在 TimedBridge 脚本中,检查所有部件都与在 Explorer 中看到的一样正确地拼写。 问题 : 无法看到零件。
确保三个部分都被锚定。
检查桥的透明属性。
创建计时器
每当玩家踩到桥时,startTimer() 会使桥变得可行走并启动计时器。一旦计时器达到 0,桥将变得不可行走,导致任何不够快的人下落下去。
使桥可行走
开始先,脚本需要使桥梁牢固或可碰撞,然后启动计时器,直到不能行走为止。
要使桥可行走,在 startTimer() 中,将桥的透明属性更改为 0 (不透明) 并将 CanCollide 属性更改为真。
local function startTimer()-- 使桥梁可见且可行走bridge.Transparency = 0bridge.CanCollide = trueend要创建一个计时器,计下倒计时,创建以下值的 for 循环。
- 控制变量 :命名为count,设置为timerDuration。
- 结束 : 0
- 增量 : -1
local function startTimer()-- 使桥梁可见且可行走bridge.Transparency = 0bridge.CanCollide = truefor count = timerDuration, 0, -1 doendend要向玩家显示计时器,将 timerText 中的文本更改为输入 timerText.Text = count 以显示计数。每次循环 for 通过循环,它都会向玩家显示计时器中的下一个数字。
for count = timerDuration, 0, -1 dotimerText.Text = countend使用一个 task.wait 函数来使 for 循环仅一次每秒运行。
for count = timerDuration, 0, -1 dotimerText.Text = counttask.wait(1)end运行游戏。当您触摸按钮时,桥应出现并启动计时器,然后完开始。
保持桥不重启
注意,如果你在按钮上移动,计时器将继续重启。
这是因为每次你触摸按钮时,for循环都会被调用,从一开始就启动for循环。为了防止计时器不断重启,你需要添加一个 boolean,一个类型的变量,来控制是否允许 startTimer() 再次调用。 布林变量 以相同的方式写入其他变量,但不使用数字或字符串,只能设置为真或假。在这种情况下,脚本将使用一个 boolean 来检查计时器是否正在运行,然后才启动它。
在你的脚本顶部,在你的变量下,创建一个名为 timerActive 的变量,并将其设置为 false 因为没有人按下按钮。
local timerDuration = 5local timerActive = falselocal function startTimer()为了确保计时器仅在 timerActive boolean 为 false 时才启动,在 buttonPressed() 中添加第二个条件到 if 声明。
local function buttonPressed(otherPart)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid and timerActive == false thenstartTimer()endend为了防止定时器到期前再次运行,将 boolean 设置为真。
local function startTimer()timerActive = truebridge.Transparency = 0bridge.CanCollide = true在 startTimer() 函数中, 在 for 循环后,将桥的透明度设置为 0.8 并将 CanCollide 设置为 false,将桥返回到其原始属性。
local function startTimer()timerActive = truebridge.Transparency = 0bridge.CanCollide = true-- 对于计时器时间倒计时的循环for count = timerDuration, 0, -1 dotimerText.Text = counttask.wait(1)end-- 使桥不能步行bridge.Transparency = 0.8bridge.CanCollide = falseend最后要重置桥的一件事是将 timerText 更改为与最初相同的空字符串然后,将 timerActive 布ool设置为 false。
bridge.Transparency = 0.8bridge.CanCollide = falsetimerText.Text = ""timerActive = false进行游戏测试以确保桥可以多次使用。
完成定时桥脚本
local bridge = script.Parent
-- 获取按钮,因为它在 Explorer 中输入
local button = workspace.ButtonBridge
-- 获取显示的部分
local timerPart = workspace.TimerDisplay
-- 获取将显示计时器的文本
local timerText = timerPart.SurfaceGui.TextLabel
-- 玩家需要多久才能穿过桥
local timerDuration = 5
local timerActive = false
local function startTimer()
print("Countdown started")
timerActive = true
bridge.Transparency = 0
bridge.CanCollide = true
-- 对于计时器时间倒计时的循环
for count = timerDuration, 0, -1 do
timerText.Text = count
task.wait(1)
end
-- 使桥不能步行
bridge.Transparency = 0.8
bridge.CanCollide = false
timerText.Text = ""
timerActive = false
end
local function buttonPressed(partTouched)
local character = partTouched.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
print("part touched")
if humanoid and timerActive == false then
print("starting timer")
startTimer()
end
end
button.Touched:Connect(buttonPressed)
摘要
循环可以与不同的交互方式结合,创建有趣的游戏时刻。在本教程中,使用倒计时循环创建了一个时间桥。在倒计时期间,玩家有限时间可以穿越。