現在你已經擁有了一個 3D 世界,是時候添加你的第一個腳本了。在這一部分中,你將創建一個硬幣收集機制,讓玩家可以收集硬幣,並在硬幣被收集後暫時禁用它們。
創建硬幣
在你可以編寫任何腳本之前,你需要在世界中擁有佔位物件作為你的硬幣。就像在前一部分中製作的海堆平台一樣,硬幣可以是簡單的 Part 物件。
創建硬幣
在 Workspace > World 中,創建一個名為 Coins 的新資料夾。
在 Coins 資料夾中,創建一個圓柱形 Part,命名為 Coin,BrickColor 設置為 Gold,Material 設置為 Metal,Size 設置為 0.6, 8, 4,Orientation 設置為 90, 0, 0。
禁用 CanCollide 並啟用 Anchored。
然後將 Coin 部件複製幾次,並將複製品散佈在島上和平台上,以便從海堆層級可以到達。
保持每個複製品的名稱為 Coin。創建腳本
接下來,你將使硬幣對玩家做出反應。Roblox 引擎可以檢測到當某物觸碰到硬幣時,但你需要使用腳本來定義硬幣應該如何反應。
創建 CoinService
在 ServerScriptService 中,創建一個名為 CoinService 的腳本,並使用以下代碼:
```lua
-- 初始化服務和變數
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local coinsFolder = Workspace.World.Coins
local coins = coinsFolder:GetChildren()
local COOLDOWN = 10
-- 定義事件處理程序
local function onCoinTouched(otherPart, coin)
if coin:GetAttribute("Enabled") then
local character = otherPart.Parent
local player = Players:GetPlayerFromCharacter(character)
if player then
-- 玩家觸碰了硬幣
coin.Transparency = 1
coin:SetAttribute("Enabled", false)
print("玩家收集了硬幣")
task.wait(COOLDOWN)
coin.Transparency = 0
coin:SetAttribute("Enabled", true)
end
end
end
-- 設置事件監聽器
for _, coin in coins do
coin:SetAttribute("Enabled", true)
coin.Touched:Connect(function(otherPart)
onCoinTouched(otherPart, coin)
end)
end
```現在,每當玩家觸碰到硬幣時,硬幣會消失 10 秒,並且輸出日誌會打印 玩家收集了硬幣。
代碼解釋
以下部分將更詳細地描述腳本的工作原理。
初始化服務和變數
就像你可能在其他語言中編寫的許多代碼一樣,你在腳本的頂部定義了稍後需要的變數。我們的代碼執行以下操作:
- 獲取所有硬幣的引用 - 然後,腳本查詢 3D 工作區以獲取所有硬幣物件的引用,使用 GetChildren() 方法。該方法返回一個數組,包含所有與其關聯的物件的父級,在這種情況下是你之前創建的 Workspace.World.Coins 資料夾。
- 定義全局變數 - COOLDOWN 變數稍後用於定義收集硬幣後禁用硬幣的時間長度。
初始化服務和變數
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local coinsFolder = Workspace.World.Coins
local coins = coinsFolder:GetChildren()
local COOLDOWN = 10
...定義事件處理程序
Roblox 引擎物理模擬 3D 世界並處理許多與渲染、物理和網絡相關的事件邏輯。當你有興趣在這些事件中編寫自己的邏輯時,你可以監聽和處理它們,同時讓引擎處理其餘部分。在這種情況下,你監聽和處理與硬幣被觸碰相關的事件。腳本在 onCoinTouched() 方法中定義了處理此事件的邏輯,該方法執行以下操作:
- 檢測玩家是否觸碰了硬幣 - 如果硬幣啟用,該方法使用玩家服務檢查觸碰硬幣的物件是否確實是玩家。當觸碰事件發生時,Roblox 引擎將觸碰硬幣的物件作為 otherPart 參數傳遞。腳本檢查 otherPart 的父級是否屬於玩家。
- 如果玩家觸碰了硬幣,則禁用硬幣,並在 10 秒後重新啟用 - 最後,如果玩家觸碰了硬幣,該方法禁用硬幣,等待 10 秒,然後重新啟用硬幣以供收集。使用 task.wait() 而不是 wait(),因為它提供了更好的性能,不會完全暫停代碼執行,允許其他線程中的任務同時運行。
定義事件處理程序
local function onCoinTouched(otherPart, coin)
if coin:GetAttribute("Enabled") then
local character = otherPart.Parent
local player = Players:GetPlayerFromCharacter(character)
if player then
-- 玩家觸碰了硬幣
coin.Transparency = 1
coin:SetAttribute("Enabled", false)
print("玩家收集了硬幣")
task.wait(COOLDOWN)
coin.Transparency = 0
coin:SetAttribute("Enabled", true)
end
end
end連接事件處理程序
所有模擬的 3D 物件都繼承自 BasePart,因此都有一個 Touched() 事件。以下循環將 onTouchedEvent() 處理程序連接到每個硬幣的觸碰事件,具體步驟如下:
- 遍歷所有硬幣 - 使用一般迭代遍歷每個硬幣。
- 將處理程序連接到事件 - 在循環的每次迭代中,硬幣默認啟用,因此在遊戲開始時在 3D 世界中可見。onCoinTouched() 處理方法也連接到硬幣的 Touched 事件,以便每次事件發生時都運行。當引擎檢測到觸碰時,它還會傳遞觸碰該物件的物件 otherPart。
連接事件處理程序
for _, coin in coins do
coin:SetAttribute("Enabled", true)
coin.Touched:Connect(function(otherPart)
onCoinTouched(otherPart, coin)
end)
end測試機制
從下拉菜單中選擇 Test 並點擊 Play。移動你的角色去觸碰一個硬幣。如果一切正常,硬幣會消失,並且 Output 窗口會打印 玩家收集了硬幣,硬幣在 10 秒後重新出現。
