Menu
CoddyTech

移动激光

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

Lasers 阶段有两面红墙,标签分别是 Slider 和 Kill,它们会沿着走道滑动并返回。每面墙都有一个属性 Slide,它是一个 Vector3,表示墙从起点移动到的位置。

补间可以重复播放。TweenInfo.new 接受时间、缓动样式、方向、重复次数(-1 表示无限次),以及是否反向播放:

local info = TweenInfo.new(2.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true)
TweenService:Create(part, info, { Position = part.Position + part:GetAttribute("Slide") }):Play()

Linear 能保持速度恒定,让玩家可以判断移动时机。默认缓动(Quad、Out)会在到达时减速,很难把握时机。

challenge icon

挑战

中等

让激光墙沿着走道滑动,然后再滑回来。

  1. 在靠近顶部的位置获取 TweenService。
  2. 编写 makeSlide(part):让部件的 Position 补间到它的 Position 加上 Slide 属性的值,耗时 Settings.SlideTime 秒,采用 Linear 方式,无限重复(-1)并反向播放。
  3. 在 setupStage 中,为每个带有 Slider 标签的部件调用它。

按下 “开始”:两面红色墙壁会以匀速来回滑动。然后按下 “检查”。

亲手试试

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

-- 第 9 课:在这里添加滑动激光

-- 旋转障碍物每帧都会转动一点
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
	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

buildTower()

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

自己练习: Roblox 在线编辑器