TweenService

Pokaż przestarzałe

*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.

Brak możliwości tworzenia
Usługa

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.

Tween Creation

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.

Looping a Tween

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.

Pausing a Tween

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

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

instance: Instance

The Instance którego właściwości mają być przetwarzane.

Wartość domyślna: ""
tweenInfo: TweenInfo

The TweenInfo do użycia.

Wartość domyślna: ""
propertyTable: Dictionary

Słownik właściwości i ich wartości docelowych, które mają być przekroczone.

Wartość domyślna: ""

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.

Tween Creation

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.

Looping a Tween

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

alpha: number

Wartość interpolacji pomiędzy 0 a 1.

Wartość domyślna: ""
easingStyle: Enum.EasingStyle

Styl łagodzenia do użycia.

Wartość domyślna: ""
easingDirection: Enum.EasingDirection

Kierunek łagodzenia do użycia.

Wartość domyślna: ""

Zwroty

Nowa wartość alfa generowana z danym stylem łagodzenia i kierunkiem.

SmoothDamp

Zapis równoległy

Zwraca tuple, który pozwala wygładzić wartość w kierunku celu, symulując krytycznie ograniczoną sprężynę.Wspiera Vector2 , Vector3 , CFrame , i numer.

Parametry

current: Variant

Obecna pozycja.

Wartość domyślna: ""
target: Variant

Pozycja docelowa.

Wartość domyślna: ""
velocity: Variant

Początkowa prędkość, przy której obecna pozycja powinna zbliżyć się do docelowej pozycji.

Wartość domyślna: ""
smoothTime: number

Czas, w którym powinna odbyć miejscecała operacja wygładzania.

Wartość domyślna: ""
maxSpeed: number

Maksymalna prędkość, przy której obecna pozycja powinna zbliżyć się do docelowej pozycji.

Wartość domyślna: ""
dt: number

Szybkość, z jaką należy stosować operację wygładzania.

Wartość domyślna: ""

Zwroty

Nowa pozycja i prędkość obliczona z operacji wygładzania.

Zdarzenia