TweenService
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
TweenService jest używany do tworzenia Tweens, które wypełniają lub przerywają właściwości instancji.można go używać na dowolnym obiekcie z kompatybilnymi typami właściwości, w tym:
TweenService:Create() , główna funkcja konstruktora, bierze TweenInfo specyfikacje dotyczące nastolatka i generuje obiekt Tween, który można następnie użyć do odtwarzania nastolatka.
Zauważ, że Tweens może wstawiać wiele właściwości jednocześnie, ale nie mogą one wstawiać tej samej właściwości.Jeśli dwóch nastolatków próbuje zmodyfikować tę samą właściwość, początkowy nastolatek zostanie anulowany i zastąpiony przez najnowszy nastolatek.
Przykłady kodu
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()
Podsumowanie
Metody
Tworzy nowy Tween dany obiekt, których właściwości mają być przetwarzane, TweenInfo , i słownik wartości właściwości celu.
- GetValue(alpha : number,easingStyle : Enum.EasingStyle,easingDirection : Enum.EasingDirection):number
Oblicza nową alfa podając Enum.EasingStyle i Enum.EasingDirection.
- SmoothDamp(current : Variant,target : Variant,velocity : Variant,smoothTime : number,maxSpeed : number?,dt : number?):Tuple
Oblicza wartość symulującą krytycznie amortyzowaną sprężynę.
Właściwości
Metody
Create
Ten konstruktor tworzy nowy Tween z trzech argumentów: obiekt do przejścia, specyfikacje TweenInfo i tabelę zawierającą właściwości do przejścia i wartości do przejścia.
Parametr propertyTable musi być słownikiem, w którym klucze są nazwami strun właściwości (na przykład Position , Transparency lub Color ), a wartości są celami właściwości na końcu tweena.
Tworzony za pomocą tej funkcji Tween utwórzony za pomocą tej funkcji jest unikalny dla obiektu podanego jako parametr instance.Aby zastosować ten sam odrost do innego obiektu, ponownie wezwij tę funkcję z nowym obiektem.
Parametry
Słownik właściwości i ich wartości docelowych, które mają być przekroczone.
Zwroty
Przykłady kodu
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
Zwraca nową wartość alfa do interpolacji za pomocą podanego wartości alfa, Enum.EasingStyle , i Enum.EasingDirection .Podany wartość alpha zostanie przyciśnięty między 0 i 1.
Parametry
Wartość interpolacji pomiędzy 0 a 1.
Styl łagodzenia do użycia.
Kierunek łagodzenia do użycia.
Zwroty
Nowa wartość alfa generowana z danym stylem łagodzenia i kierunkiem.
SmoothDamp
Zwraca tuple, który pozwala wygładzić wartość w kierunku celu, symulując krytycznie ograniczoną sprężynę.Wspiera Vector2 , Vector3 , CFrame , i numer.
Parametry
Obecna pozycja.
Pozycja docelowa.
Początkowa prędkość, przy której obecna pozycja powinna zbliżyć się do docelowej pozycji.
Czas, w którym powinna odbyć miejscecała operacja wygładzania.
Maksymalna prędkość, przy której obecna pozycja powinna zbliżyć się do docelowej pozycji.
Szybkość, z jaką należy stosować operację wygładzania.
Zwroty
Nowa pozycja i prędkość obliczona z operacji wygładzania.