Menu
CoddyTech

לייזרים מחליקים

שיעור 8 מתוך 18 בקורס משחק Roblox: מגדל אינסופי של Coddy.

בשלב Lasers יש שני קירות אדומים, שמתויגים Slider ו-Kill, המחליקים לאורך השביל ובחזרה. לכל אחד מהם יש מאפיין, Slide, מסוג Vector3 שמציין לאן הוא נע מהמקום שבו הוא מתחיל.

אפשר לחזור על tween. 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 שומר על מהירות אחידה, כך ששחקן יכול להעריך אותה. פונקציית ההקלה המוגדרת כברירת מחדל מאטה בכל קצה, מה שנראה חלק אבל מקשה על תזמון.

challenge icon

אתגר

בינוני
  1. בחלק העליון, קבל את TweenService.
  2. כתוב את makeSlide(part): בצע tween ל-Position של החלק, כך שיהיה שווה ל-Position שלו ועוד המאפיין Slide, במשך Settings.SlideTime שניות, באופן ליניארי, בחזרה אינסופית ובהיפוך.
  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

-- האלמנטים המסתובבים מסתובבים מעט בכל פריים
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()
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

כל השיעורים ביחידה משחק Roblox: מגדל אינסופי