TweenService

Afficher les obsolètes

*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.

Création impossible
Service

TweenService est utilisé pour créer Tweens qui interpolent ou tweenles propriétés des instances.Tweens peut être utilisé sur n'importe quel objet avec des types de propriété compatibles, y compris :

TweenService:Create() , la fonction de constructeur principale, prend TweenInfo des spécifications au sujet du préadolescent et génère l'objet Tween qui peut ensuite être utilisé pour jouer le tween.

Notez que Tweens peut interpoler plusieurs propriétés en même temps, mais elles ne doivent pas interpoler la même propriété.Si deux adolescents tentent de modifier la même propriété, le tween initial sera annulé et remplacé par le tween le plus récent.

Échantillons de code

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()

Résumé

Méthodes

Propriétés

Méthodes

Create

Ce constructeur crée un nouveau Tween à partir de trois arguments : l'objet à tween, les spécifications TweenInfo et une table contenant les propriétés à transférer et les valeurs à transférer.

Le paramètre propertyTable doit être un dictionnaire où les clés sont les noms de chaîne de la propriété (par exemple Position , Transparency ou Color ), et les valeurs sont les cibles de propriété à la fin du tween.

Le Tween créé en utilisant cette fonction est unique pour l'objet donné en tant que paramètre instance.Pour appliquer le même tween à un autre objet, appelez cette fonction à nouveau avec le nouvel objet.

Paramètres

instance: Instance

Le Instance dont les propriétés doivent être transférées.

Valeur par défaut : ""
tweenInfo: TweenInfo

Le TweenInfo à utiliser.

Valeur par défaut : ""
propertyTable: Dictionary

Un dictionnaire de propriétés et de leurs valeurs cibles, à transmettre.

Valeur par défaut : ""

Retours

Échantillons de code

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

Renvoie une nouvelle valeur alpha pour l'interpolation en utilisant la valeur alpha donnée, Enum.EasingStyle , et Enum.EasingDirection .La valeur fournie alpha sera comprise entre 0 et 1.

Paramètres

alpha: number

Une valeur d'interpolation entre 0 et 1 .

Valeur par défaut : ""
easingStyle: Enum.EasingStyle

Le style de relâchement à utiliser.

Valeur par défaut : ""
easingDirection: Enum.EasingDirection

La direction de relâchement à utiliser.

Valeur par défaut : ""

Retours

Une nouvelle valeur alpha générée à partir du style et de la direction de relâchement donnés.

SmoothDamp

Écrire en parallèle

Renvoie une tuplé qui permet de lisser une valeur vers une cible, simulant une ressort critiquement amortie.Prend en charge Vector2 , Vector3 , CFrame , et le nombre.

Paramètres

current: Variant

La position actuelle.

Valeur par défaut : ""
target: Variant

La position cible.

Valeur par défaut : ""
velocity: Variant

La vitesse initiale à laquelle la position actuelle devrait approcher la position cible.

Valeur par défaut : ""
smoothTime: number

La durée pendant laquelle l'opération de lissage total devrait avoir emplacement.

Valeur par défaut : ""
maxSpeed: number

La vitesse maximale à laquelle la position actuelle devrait approcher la position cible.

Valeur par défaut : ""
dt: number

Le taux auquel l'opération de lissage devrait être appliquée.

Valeur par défaut : ""

Retours

La nouvelle position et la vitesse calculées à partir de l'opération de lissage.

Évènements