TweenService
*Dieser Inhalt wurde mit KI (Beta) übersetzt und kann Fehler enthalten. Um diese Seite auf Englisch zu sehen, klicke hier.
TweenService wird verwendet, um Tweens zu erstellen, die die Eigenschaften der Instanzen interpolieren oder tweenen. Tweens kann auf jedem Objekt mit kompatiblen Eigenschaftstypen verwendet werden, einschließlich:
TweenService:Create() , die Hauptbaumeisterungsfunktion, nimmt TweenInfo Spezifikationen über den Tween und generiert das Class.Tween -Objekt, das dann verwendet werden kann, um den Tween abzuspielen.
Beachten Sie, dass Tweens mehrere Eigenschaften gleichzeitig interpolieren kann, aber sie müssen nicht dasselbe Eigenschaft interpolieren. Wenn zwei Tweens versuchen, dasselbe Eigenschaft zu ändern, wird das ursprüngliche Tween abgebrochen und überschrieben durch das neueste Tweens.
Code-Beispiele
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()
Zusammenfassung
Methoden
Erstellt ein neues Tween mit den Eigenschaften des zu tweenenden Objekts, einer TweenInfo und einer Datenbank von Ziel-Eigenschaften.
- GetValue(alpha : number,easingStyle : Enum.EasingStyle,easingDirection : Enum.EasingDirection):number
Berechnet eine neue Alpha, die eine Enum.EasingStyle und eine Enum.EasingDirection hat.
- SmoothDamp(current : Variant,target : Variant,velocity : Variant,smoothTime : number,maxSpeed : number?,dt : number?):Tuple
Eigenschaften
Methoden
Create
Der Create() erstellt einen neuen Tween aus drei Argumenten: das Objekt zum Tweeln, die Spezifikationen von TweenInfo und eine Tabelle mit den Eigenschaften zum Tweeln und Werten zum Tweeln.
Der propertyTable-Parameter muss ein Wörterbuch sein, in dem die Schlüssel die Stringsamen der Eigenschaft sind (z. B. Position, Transparency oder 1>Color1>), und die Werte sind die Ziel-Eigenschaften am Ende des Tween.
Das Tween , das mit dieser Funktion erstellt wurde, ist einzigartig für das Objekt, das als instance -Parameter gegeben wird. Um dieselbe Tween auf ein anderes Objekt anzuwenden, rufe diese Funktion erneut mit dem neuen Objekt auf.
Parameter
Ein Wörterbuch von Eigenschaften und ihren Zielwerten, die tweened werden sollen.
Rückgaben
Code-Beispiele
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
Gibt einen neuen Alpha-Wert für die Interpolation mit dem angegebenen Alpha-Wert zurück, Enum.EasingStyle und Enum.EasingDirection. Der angegebene alpha -Wert wird zwischen 1> 01> und 4> 14> geklemmt.
Parameter
Ein Interpolation-Wert zwischen 0 und 1 .
Der Entlastungsstil, den Sie verwenden.
Die Entlastungsrichtung, die verwendet wird.
Rückgaben
Ein neuer Alpha-Wert, der aus dem gegebenen Stil und der Richtung erzeugt wird.