ประเภทข้อมูลของ Engine
Collection
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ข้อมูลสรุป
ตัวอย่างโค้ด
วงจรชีวิตและเหตุการณ์ของการเก็บรวบรวม
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.Material = Enum.Material.Plastic
end
-- การเรียกกลับของ RunService: จะถูกเรียกครั้งต่อครั้งในแต่ละจังหวะสำหรับใบไม้แต่ละใบในคอลเลกชัน
function mapleLeaves.OnHeartbeat(leaf: Part, deltaTime: number)
sway(leaf, deltaTime)
end
-- การเรียกกลับของอีเวนต์: เชื่อมต่อกับ BasePart.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
คุณสมบัติ
วิธีการ
Destroy
Collection:Destroy()