Menu
CoddyTech

上升的熔岩

Coddy Roblox 游戏:无尽高塔 课程的第 11 课,共 21 课。

在底部等待并不安全。一片熔岩从塔下方开始上升,经过 Settings.LavaDelay 秒后,它会匀速上升,直到到达塔顶下方 2 studs 的位置,所以所有人都得继续向上爬。

补间可以在开始前等待:TweenInfo.new 的第六个参数是以秒为单位的延迟时间。要让它匀速上升,补间时长等于距离除以速度:

-- 等待 4 秒后,以每秒 3 studs 的速度移动 60 studs
local info = TweenInfo.new(60 / 3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 4)

熔岩不是实心的(CanCollide 为 false),所以没人能站在上面;它的 Touched 使用 killOnTouch,也就是和杀伤砖相同的函数。它从高度 -2 处开始,上升到 top - 2,正好上升 top studs。

challenge icon

挑战

中等

熔岩在塔下方上升。

  1. 让 buildTower 返回 top,并以 startLava(buildTower()) 结束脚本。
  2. 编写 startLava(top):将顶层的 lava 设为一个已锚定、名为 Lava 的 Part,CanCollide 设为 false,位置设为 (0, -2, 0),并将 Touched 连接到 killOnTouch。
  3. 让它在 top / Settings.LavaSpeed 秒内缓动到 (0, top - 2, 0),使用 Linear,并延迟 Settings.LavaDelay。

按下“开始”:10 秒后,熔岩会在你下方开始上升。然后按“检查”。

亲手试试

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
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)

-- 碰到带有 Kill 标签的物体就会失去一条命
local function killOnTouch(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

-- 有人踩上会消失的台阶后,它很快会变淡并消失,之后恢复原状
local function makeVanish(part)
	part.Touched:Connect(function(hit)
		if part:GetAttribute("Gone") or not hit.Parent:FindFirstChild("Humanoid") then
			return
		end
		part:SetAttribute("Gone", true)
		task.wait(Settings.VanishDelay)
		part.Transparency = 0.8
		part.CanCollide = false
		task.wait(Settings.VanishTime)
		part.Transparency = 0
		part.CanCollide = true
		part:SetAttribute("Gone", nil)
	end)
end

-- 激光会不断滑向 Slide 指定的偏移位置,再滑回来
local function makeSlide(part)
	local info = TweenInfo.new(Settings.SlideTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true)
	local goal = { Position = part.Position + part:GetAttribute("Slide") }
	TweenService:Create(part, info, goal):Play()
end

-- 弹跳垫会把踩上去的人弹到空中
local function bounce(hit)
	local root = hit.Parent:FindFirstChild("HumanoidRootPart")
	if root then
		root.AssemblyLinearVelocity = Vector3.new(0, Settings.BouncePower, 0)
	end
end

-- 旋转物体每帧都会转动一点
RunService.Heartbeat:Connect(function(dt)
	for _, spinner in CollectionService:GetTagged("Spinner") do
		if spinner:IsDescendantOf(workspace) then
			spinner:PivotTo(spinner:GetPivot() * CFrame.Angles(0, math.rad(Settings.SpinSpeed) * dt, 0))
		end
	end
end)

-- 根据标签启用关卡中的所有障碍物
local function setupStage(stage)
	for _, part in stage:GetDescendants() do
		if part:HasTag("Kill") then
			part.Touched:Connect(killOnTouch)
		end
		if part:HasTag("Vanish") then
			makeVanish(part)
		end
		if part:HasTag("Conveyor") then
			part.AssemblyLinearVelocity = part.CFrame.LookVector * Settings.BeltSpeed
		end
		if part:HasTag("Slider") then
			makeSlide(part)
		end
		if part:HasTag("Bounce") then
			part.Touched:Connect(bounce)
		end
	end
end

-- 随机选取一个关卡
local function pickStage()
	local all = stages:GetChildren()
	return all[math.random(1, #all)]
end

-- 把模型的副本放在塔顶,返回副本和新的塔顶高度
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

-- 从下往上建造一座新的随机塔
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(pickStage(), top)
		top = newTop
		setupStage(stage)
	end
	stack(ServerStorage.Finish, top)
end

-- 第 11 课:Rising Lava 写在这里

buildTower()
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

Roblox 游戏:无尽高塔中的所有课

自己练习: Roblox 在线编辑器