Percorri il sentiero
Lezione 3 di 22 del corso Gioco Roblox: Tower Defense di Coddy.
Un Humanoid cammina con MoveTo, e il suo evento MoveToFinished viene attivato quando arriva. Si arrende dopo 8 secondi e viene attivato con false, quindi un nemico lento su un lungo tratto viene inviato di nuovo finché non arriva:
repeat
humanoid:MoveTo(points[4].Position)
until humanoid.MoveToFinished:Wait()Percorrere tutto il tragitto richiede tempo, quindi walkPath viene eseguita nel proprio thread con task.spawn(walkPath, enemy), mentre spawnEnemy prosegue e restituisce il controllo.
Sfida
FacileFai camminare lo Zombie lungo il percorso.
- Scrivi
walkPath(enemy): peri = 2, #waypoints, ripetienemy.Humanoid:MoveTo(waypoints[i].Position)finchéMoveToFinished:Wait()indica che è arrivato oppurenot enemy.Parent; restituisci il controllo se il nemico non c’è più. - In
spawnEnemy, prima direturn enemy, aggiungitask.spawn(walkPath, enemy).
Premi «Gioca»: lo Zombie percorre il sentiero fino alla base blu. Poi premi «Verifica».
Provalo tu
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local enemies = workspace.Enemies
print("The base can take " .. Settings.BaseHealth .. " hits")
-- I waypoint, in ordine (si chiamano 1, 2, 3...)
local waypoints = workspace.Path.Waypoints:GetChildren()
table.sort(waypoints, function(a, b)
return tonumber(a.Name) < tonumber(b.Name)
end)
-- Ogni giocatore riceve Money nella classifica
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = Settings.StartMoney
money.Parent = leaderstats
end)
-- Lezione 3: Walk the Path va qui
-- Un nuovo nemico al cancello, che percorre il sentiero
local function spawnEnemy(kind)
local enemy = ServerStorage.Enemies[kind]:Clone()
-- Posizionato appena sopra il primo waypoint
enemy:PivotTo(waypoints[1].CFrame + Vector3.new(0, 5, 0))
enemy.Parent = enemies
return enemy
end
-- Un solo nemico, per ora
spawnEnemy("Zombie")
Tutte le lezioni di Gioco Roblox: Tower Defense
Esercitati da solo: Playground Roblox