現在你已經擁有了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.Coinslocal coins = coinsFolder:GetChildren()local COOLDOWN = 10-- 定義事件處理程序local function onCoinTouched(otherPart, coin)if coin:GetAttribute("Enabled") thenlocal character = otherPart.Parentlocal player = Players:GetPlayerFromCharacter(character)if player then-- 玩家碰觸到硬幣coin.Transparency = 1coin:SetAttribute("Enabled", false)print("玩家收集了硬幣")task.wait(COOLDOWN)coin.Transparency = 0coin:SetAttribute("Enabled", true)endendend-- 設定事件監聽器for _, coin in coins docoin: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.Coinslocal 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秒後會重新出現。
