随机高塔
Coddy Roblox 游戏:无尽高塔 课程的第 4 课,共 21 课。
连续五次出现同一种阶段的塔一点也不好玩。ServerStorage.Stages 里有七种阶段:Steps、KillBricks、Conveyor、VanishingSteps、Bounce、Spinner 和 Lasers。每层随机挑选一种:
local all = stages:GetChildren()
local stage = all[math.random(1, #all)]math.random(1, #all) 会生成一个从 1 到阶段数量的整数,因此每种阶段被选中的概率都相同。它们大多还没有任何作用:其中的旋转器、传送带和激光会在下一章启动。
最后一个阶段上方放着 ServerStorage.Finish,这是攀爬终点处的金色垫子。stack 已经可以抬起任何 Model,所以它也会抬起 Finish。
挑战
简单用随机选择的关卡搭建每座塔,并在顶部放上终点。
- 编写
pickStage():返回stages的一个随机子项。 - 在
buildTower中,叠放pickStage(),而不是stages.Steps。 - 循环结束后,在顶部叠放
ServerStorage.Finish。
按几次“开始”:每座塔都不一样,顶部有一个金色平台。然后按“检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local stages = ServerStorage.Stages
local tower = nil
print("Each tower has " .. Settings.StageCount .. " stages")
-- 每个玩家的排行榜上都有 Wins
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
end)
-- 第 4 课:在这里添加随机塔
-- 将模型的副本放在塔顶;返回该副本和新的塔顶高度
local function stack(template, bottom)
local copy = template:Clone()
local box, size = copy:GetBoundingBox()
local lift = bottom - (box.Position.Y - size.Y / 2)
copy:PivotTo(copy:GetPivot() + Vector3.new(0, lift, 0))
copy.Parent = tower
return copy, bottom + size.Y
end
-- 用 Steps 关卡搭建一座塔
local function buildTower()
tower = Instance.new("Model")
tower.Name = "Tower"
tower.Parent = workspace
local top = 0
for i = 1, Settings.StageCount do
local stage, newTop = stack(stages.Steps, top)
top = newTop
end
end
buildTower()
Roblox 游戏:无尽高塔中的所有课
自己练习: Roblox 在线编辑器