TweenService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
TweenService ใช้สร้าง Tweens ซึ่งจะแทรกหรือแทรกคุณสมบัติของตัวอย่างTweens สามารถใช้กับวัตถุใดก็ได้ที่มีประเภทคุณสมบัติที่สอดคล้อง รวมถึง:
- boolean สนับสนุน การสนับสนุน
TweenService:Create() , ฟังก์ชันผู้สร้างหลัก, ใช้ TweenInfo ข้อกำหนดเกี่ยวกับวัยรุ่นและสร้างวัตถุ Tween ซึ่งสามารถใช้เล่นวัยรุ่นได้
โปรดทราบว่า Tweens สามารถแทรกคุณสมบัติหลายอย่างในเวลาเดียวกัน แต่ต้องไม่แทรกคุณสมบัติเดียวกันหากวัยรุ่นสองคนพยายามที่จะแก้ไขคุณสมบัติเดียวกัน วัยรุ่นเบื้องต้นจะถูกยกเลิกและเขียนทับโดยวัยรุ่นล่าสุด
ตัวอย่างโค้ด
In this example a Tween is created to animate the position and color of a Part. Because the position and color are part of the same tween, they will change at the exact same rate and will reach their goal at the same time.
local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Color = Color3.new(1, 0, 0)
part.Anchored = true
part.Parent = game.Workspace
local goal = {}
goal.Position = Vector3.new(10, 10, 0)
goal.Color = Color3.new(0, 1, 0)
local tweenInfo = TweenInfo.new(5)
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()
This code sample includes an example of how a looped tween can be created. A part is instanced in the Workspace and a Tween is created using TweenService:Create() that is set to animate its position along the Y axis.
The looped effect is achieved by modifying the TweenInfo used in TweenService:Create(). Specifically, when RepeatCount is set to less than 0, the tween will play indefinitely. Also, setting Reverses to true will cause the tween to play in reverse once it has reached its destination. In combination this creates a looped effect.
The correct way to make a tween play indefinitely is to set RepeatCount to -1. You should avoid using large numbers or math.huge() as a substitute as this is unstable and may stop working at any point.
local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.Parent = workspace
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
true, -- Reverses (tween will reverse once reaching its goal)
0 -- DelayTime
)
local tween = TweenService:Create(part, tweenInfo, { Position = Vector3.new(0, 30, 0) })
tween:Play()
task.wait(10)
tween:Cancel() -- cancel the animation after 10 seconds
This sample demonstrates how the playback of a tween can be paused and resumed.
A part is instanced in the Workspace and a tween is setup that will move it 50 studs along the X axis. However during playback the tween is briefly paused, then resumed. To further illustrate this the BrickColor of the part changes from red to green while it is paused.
local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.BrickColor = BrickColor.new("Bright green")
part.Parent = workspace
local goal = {}
goal.Position = Vector3.new(50, 10, 0)
local tweenInfo = TweenInfo.new(10, Enum.EasingStyle.Linear)
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()
task.wait(3)
part.BrickColor = BrickColor.new("Bright red")
tween:Pause()
task.wait(2)
part.BrickColor = BrickColor.new("Bright green")
tween:Play()
สรุป
วิธีการ
สร้างใหม่ Tween โดยให้วัตถุที่มีคุณสมบัติจะถูกแบ่งระหว่างกัน, TweenInfo และสารานุกรมของค่าคุณสมบัติเป้าหมาย
- GetValue(alpha : number,easingStyle : Enum.EasingStyle,easingDirection : Enum.EasingDirection):number
คำนวณอัลฟาใหม่ที่ได้รับ Enum.EasingStyle และ Enum.EasingDirection
- SmoothDamp(current : Variant,target : Variant,velocity : Variant,smoothTime : number,maxSpeed : number?,dt : number?):Tuple
คำนวณมูลค่าที่จำลองสปริงที่ถูกดันอย่างรุนแรง
คุณสมบัติ
วิธีการ
Create
คอนสตรักเตอร์นี้สร้าง Tween ใหม่จากสามอาร์กิวเมนต์: วัตถุที่จะเปลี่ยนผ่าน, ข้อกำหนด TweenInfo และตารางที่มีคุณสมบัติที่จะเปลี่ยนผ่านและค่าที่จะเปลี่ยนผ่าน
พารามิเตอร์ propertyTable ต้องเป็นสารบัญที่คีย์เป็นชื่อสตริงของคุณสมบัติ (ตัวอย่างเช่น Position , Transparency หรือ Color ), และมูลค่าเป็นเป้าหมายของคุณสมบัติในตอนท้ายของทีน
การสร้าง Tween ที่สร้างขึ้นโดยใช้ฟังก์ชันนี้เป็นเอกลักษณ์สำหรับวัตถุที่ให้เป็นพารามิเตอร์ instanceเพื่อใช้ทีนเดียวกันกับวัตถุอื่น โทรฟังก์ชันนี้อีกครั้งด้วยวัตถุใหม่
พารามิเตอร์
สารานุกรมของคุณสมบัติและมูลค่าเป้าหมายที่จะถูกแยกออก
ส่งค่ากลับ
ตัวอย่างโค้ด
In this example a Tween is created to animate the position and color of a Part. Because the position and color are part of the same tween, they will change at the exact same rate and will reach their goal at the same time.
local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Color = Color3.new(1, 0, 0)
part.Anchored = true
part.Parent = game.Workspace
local goal = {}
goal.Position = Vector3.new(10, 10, 0)
goal.Color = Color3.new(0, 1, 0)
local tweenInfo = TweenInfo.new(5)
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()
This code sample includes an example of how a looped tween can be created. A part is instanced in the Workspace and a Tween is created using TweenService:Create() that is set to animate its position along the Y axis.
The looped effect is achieved by modifying the TweenInfo used in TweenService:Create(). Specifically, when RepeatCount is set to less than 0, the tween will play indefinitely. Also, setting Reverses to true will cause the tween to play in reverse once it has reached its destination. In combination this creates a looped effect.
The correct way to make a tween play indefinitely is to set RepeatCount to -1. You should avoid using large numbers or math.huge() as a substitute as this is unstable and may stop working at any point.
local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.Parent = workspace
local tweenInfo = TweenInfo.new(
2, -- Time
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
true, -- Reverses (tween will reverse once reaching its goal)
0 -- DelayTime
)
local tween = TweenService:Create(part, tweenInfo, { Position = Vector3.new(0, 30, 0) })
tween:Play()
task.wait(10)
tween:Cancel() -- cancel the animation after 10 seconds
GetValue
คืนค่าอัลฟาใหม่สำหรับการแทนที่โดยใช้ค่าอัลฟาที่กำหนด Enum.EasingStyle และ Enum.EasingDirectionค่าที่ให้ไว้ alpha จะถูกขัดขวางระหว่าง 0 และ 1
พารามิเตอร์
มูลค่าการคูณระหว่าง 0 และ 1 .
สไตล์การผ่อนคลายที่จะใช้
ทิศทางการผ่อนคลายที่จะใช้
ส่งค่ากลับ
มูลค่าอัลฟาใหม่ที่สร้างจากสไตล์และทิศทางการผ่อนคลายที่กำหนดไว้
SmoothDamp
ส่งคืน tuple ที่ช่วยให้สมูทค่าไปสู่เป้าหมายโดยจำลองสปริงที่ถูกกระแทกอย่างรุนแรงสนับสนุน Vector2 , Vector3 , CFrame และหมายเลข
พารามิเตอร์
ตำแหน่งปัจจุบัน
ตำแหน่งเป้าหมาย
ความเร็วเบื้องต้นที่ตำแหน่งปัจจุบันควรใกล้ตำแหน่งเป้าหมาย
สถานที่
ความเร็วสูงสุดที่ตำแหน่งปัจจุบันควรใกล้ตำแหน่งเป้าหมาย
อัตราที่การดัดผิวควรถูกใช้
ส่งค่ากลับ
ตำแหน่งและความเร็วใหม่ที่คำนวณจากการดำเนินการบรรเทา