TweenService
*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.
TweenService digunakan untuk membuat Tweens yang menggabungkan, atau menengahi, properti instans. Tweens dapat digunakan pada objek mana pun dengan jenis properti yang kompatibel, termasuk:
TweenService:Create() , fungsi konstruktor utama, mengambil TweenInfo spesifikasi tentang remaja dan menghasilkan objek Tween yang kemudian dapat digunakan untuk memainkan remaja.
Perhatikan bahwa Tweens dapat menggabungkan beberapa properti pada saat yang sama, tetapi mereka tidak boleh menggabungkan properti yang sama.Jika dua remaja mencoba memodifikasi properti yang sama, tween awal akan dibatalkan dan ditulis ulang oleh tween terbaru.
Contoh Kode
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()
Rangkuman
Metode
Membuat baru Tween karena objek yang propertinya akan disatukan, TweenInfo , dan kamus nilai properti tujuan.
- GetValue(alpha : number,easingStyle : Enum.EasingStyle,easingDirection : Enum.EasingDirection):number
Menghitung alfa baru diberi Enum.EasingStyle dan Enum.EasingDirection .
- SmoothDamp(current : Variant,target : Variant,velocity : Variant,smoothTime : number,maxSpeed : number?,dt : number?):Tuple
Menghitung nilai yang meniru musim semi kritis yang dipampas.
Properti
Metode
Create
Konstruktor ini membuat baru Tween dari tiga argumen: objek untuk tween, spesifikasi TweenInfo , dan tabel yang berisi properti untuk tween dan nilai untuk tween.
Parameter propertyTable perlu menjadi kamus di mana kunci adalah nama string properti (misalnya Position , Transparency , atau Color ), dan nilai adalah target properti di akhir tween.
The Tween yang dibuat menggunakan fungsi ini unik untuk objek yang diberikan sebagai parameter instance.Untuk menerapkan remaja yang sama ke objek lain, panggil fungsi ini lagi dengan objek baru.
Parameter
Kamus properti, dan nilai target mereka, untuk disingkirkan.
Memberikan nilai
Contoh Kode
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
Kembalikan nilai alfa baru untuk interpolasi menggunakan nilai alfa yang diberikan, Enum.EasingStyle , dan Enum.EasingDirection .Nilai yang disediakan alpha akan dikunci di antara 0 dan 1.
Parameter
Nilai interpolasi antara 0 dan 1 .
Gaya penghilangan untuk digunakan.
Arah penghapusan untuk digunakan.
Memberikan nilai
Nilai alfa baru yang dihasilkan dari gaya dan arah penyederhanaan yang diberikan.
SmoothDamp
Kembalikan tuple yang memungkinkan menyглажива nilai menuju target, menyimulasikan musim semi yang sangat kritis.Mendukung Vector2 , Vector3 , CFrame , dan nomor.
Parameter
Posisi saat ini.
Posisi target.
Kecepatan awal di mana posisi saat ini harus mendekati posisi target.
Durasi di mana operasi penyamisan total harus tempat.
Kecepatan maksimum di mana posisi saat ini harus mendekati posisi target.
Tingkat di mana operasi smoothing harus diterapkan.
Memberikan nilai
Posisi dan kecepatan baru yang dihitung dari operasi penyглаживание.