消失的桥
本课属于 Coddy Lua 学习旅程的“Roblox 游戏脚本编写”单元。第 10 课,共 25 课。
闪烁的灯改变了一部分。桥有很多节,它们都必须一起改变。
GetChildren() 会以表的形式返回对象的直接子级,可直接用于 for 循环。属于对象的函数(如 GetChildren)要用冒号调用:
for _, child in workspace.Bridge:GetChildren() do
print(child.Name)
end文件夹也可以包含你的 Script,而 Script 没有 Transparency 属性。对于每种部件,child:IsA("BasePart") 都会返回 true;对于 Script,则会返回 false。只有条件为真时,if condition then ... end 才会运行其中的语句:
if child:IsA("BasePart") then
child.Transparency = 0.8
end部件即使可以透视,仍然可能是实心的:设置 CanCollide = false 才能让玩家掉下去。
挑战
简单让桥上的四级台阶永远消失又出现。
- 将
task.wait(2)和下面的循环放在while true do ... end中。 - 在其中,在该循环之后添加另一个
task.wait(2),再添加第二个循环,将Transparency设置为0,并将CanCollide设置为true。
按下“开始”:台阶每隔 4 秒就会淡出,并在 2 秒后重新出现。然后点击“检查”。
亲手试试
local bridge = script.Parent
-- 这会让台阶在 2 秒后消失一次。
-- 让它们再过 2 秒恢复,并一直重复这个过程。
task.wait(2)
for _, step in bridge:GetChildren() do
if step:IsA("BasePart") then
step.Transparency = 0.8
step.CanCollide = false
end
end
这节课包含一个小测验。开始学习即可作答,并记录你的进度。
Roblox 游戏脚本编写中的所有课
自己练习: Lua 在线编译器