學習
引擎資料類型
Collection

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡


概要
方法
ForEach(callback: function)
範例程式碼
集合生命週期與事件
local CollectionService = game:GetService("CollectionService")
-- 一個標記為 "MapleLeaf" 的部件的即時集合,這些部件是標記為 "MapleTree" 的模型的直接子項
local mapleLeaves: Collection = CollectionService:CreateCollection("Model.MapleTree > Part.MapleLeaf")
local function sway(leaf: Part, deltaTime: number)
leaf.CFrame *= CFrame.fromEulerAnglesXYZ(0, math.rad(20) * deltaTime, 0)
end
local function fall(leaf: Part)
leaf.Anchored = false
end
-- OnAdded 對於每個已經匹配的葉子觸發一次,然後對於每個新的匹配
function mapleLeaves.OnAdded(leaf: Part)
leaf.Material = Enum.Material.Neon
end
-- OnRemoved 在葉子停止匹配時觸發(未標記、重新父級到樹外或被摧毀)
function mapleLeaves.OnRemoved(leaf: Part)
leaf:Destroy()
end
-- RunService 別名:對於集合中的每個葉子每次心跳觸發一次
function mapleLeaves.OnHeartbeat(leaf: Part, deltaTime: number)
sway(leaf, deltaTime)
end
-- 實例事件包裝器:OnTouched 包裝 BasePart.Touched
-- 沒有 Touched 事件的集合成員會靜默忽略此分配
function mapleLeaves.OnTouched(leaf: Part, other: BasePart)
fall(leaf)
end
-- 屬性變更包裝器:當葉子變成棕色時反應
function mapleLeaves.OnPropertyChanged.Color(leaf: Part)
if leaf.Color == Color3.fromRGB(124, 92, 70) then
fall(leaf)
end
end
-- 以命令式方式將當前在集合中的每個葉子設置為綠色
mapleLeaves:ForEach(function(leaf: Part)
leaf.Color = Color3.fromRGB(0, 255, 0)
end)

API 參考
屬性
Enabled
Collection.Enabled:boolean

方法
ForEach
Collection:ForEach(callback:function)
參數
callback:function

Destroy
Collection:Destroy()

©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。