Loại dữ liệu công cụ
Collection
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Mẫu mã
Vòng đời và Sự kiện của Bộ sưu tập
local CollectionService = game:GetService("CollectionService")
-- Một bộ sưu tập trực tiếp của các phần được gán nhãn "MapleLeaf" mà là con trực tiếp của một mô hình được gán nhãn "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 được gọi một lần cho mỗi lá đã khớp, sau đó cho mỗi khớp mới
function mapleLeaves.OnAdded(leaf: Part)
leaf.Material = Enum.Material.Neon
end
-- OnRemoved được gọi khi một lá ngừng khớp (không gán nhãn, được chuyển ra khỏi cây, hoặc bị phá hủy)
function mapleLeaves.OnRemoved(leaf: Part)
leaf:Destroy()
end
-- RunService alias: Gọi một lần cho mỗi nhịp tim cho mỗi lá trong bộ sưu tập
function mapleLeaves.OnHeartbeat(leaf: Part, deltaTime: number)
sway(leaf, deltaTime)
end
-- Instance-event wrapper: OnTouched bọc BasePart.Touched
-- Các thành viên bộ sưu tập không có sự kiện Touched sẽ lặng lẽ bỏ qua sự gán này
function mapleLeaves.OnTouched(leaf: Part, other: BasePart)
fall(leaf)
end
-- Property-changed wrapper: Phản ứng khi một lá chuyển sang màu nâu
function mapleLeaves.OnPropertyChanged.Color(leaf: Part)
if leaf.Color == Color3.fromRGB(124, 92, 70) then
fall(leaf)
end
end
-- Thiết lập một cách rõ ràng mọi lá hiện có trong bộ sưu tập thành màu xanh
mapleLeaves:ForEach(function(leaf: Part)
leaf.Color = Color3.fromRGB(0, 255, 0)
end)Tài Liệu Tham Khảo API
Thuộc Tính
Phương Pháp
Destroy
Collection:Destroy()