TweenService

Visualizza obsoleti

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.

Non costruibile
Assistenza

TweenService viene utilizzato per creare Tweens che interpola o gemellatile proprietà delle istanze.Tweens può essere utilizzato su qualsiasi oggetto con tipi di proprietà compatibili, tra cui:

TweenService:Create() , la funzione costruttore primaria, prende TweenInfo specifiche sul tween e genera l'oggetto Tween che può quindi essere utilizzato per giocare al gemellati.

Nota che Tweens può interpolare più proprietà allo stesso tempo, ma non devono essere interpolate la stessa Proprietà.Se due adolescenti tentano di modificare la stessa Proprietà, il tween iniziale verrà annullato e sovrascritto dal gemellatipiù recente.

Campioni di codice

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

Sommario

Metodi

Proprietà

Metodi

Create

Questo costruttore crea un nuovo Tween da tre argomenti: l'oggetto da gemellati, le specifiche TweenInfo e una tabella che contiene le proprietà da tween e i valori da tween.

Il parametro propertyTable deve essere un dizionario in cui le chiavi sono i nomi delle proprietà delle stringhe (ad esempio Position , Transparency , o Color ), e i valori sono gli obiettivi della proprietà alla fine del gemellati.

Il Tween creato utilizzando questa funzione è unico per l'oggetto dato come parametro instance.Per applicare lo stesso tween a un altro oggetto, chiama di nuovo questa funzione con il nuovo oggetto.

Parametri

instance: Instance

Il Instance le cui proprietà devono essere transitate.

Valore predefinito: ""
tweenInfo: TweenInfo

Il TweenInfo da utilizzare.

Valore predefinito: ""
propertyTable: Dictionary

Un dizionario di proprietà e dei loro valori target, da mettere in mezzo.

Valore predefinito: ""

Restituzioni

Campioni di codice

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

Restituisce un nuovo valore alpha per l'interpolazione utilizzando il valore alpha fornito, Enum.EasingStyle , e Enum.EasingDirection .Il valore fornito alpha verrà incastrato tra 0 e 1.

Parametri

alpha: number

Un valore di interpolazione tra 0 e 1 .

Valore predefinito: ""
easingStyle: Enum.EasingStyle

Lo stile di allentamento da utilizzare.

Valore predefinito: ""
easingDirection: Enum.EasingDirection

La direzione di allentamento da utilizzare.

Valore predefinito: ""

Restituzioni

Un nuovo valore alpha generato dallo stile e dalla direzione di allentamento data.

SmoothDamp

Scrivi Parallelo

Restituisce un tuple che consente di levigare un valore verso un bersaglio, simulando una molla criticamente ammortizzata.Supporta Vector2 , Vector3 , CFrame e numero.

Parametri

current: Variant

La posizione attuale.

Valore predefinito: ""
target: Variant

La posizione target.

Valore predefinito: ""
velocity: Variant

La velocità iniziale a cui la posizione attuale dovrebbe avvicinarsi alla posizione target.

Valore predefinito: ""
smoothTime: number

La durata in cui dovrebbe Postol'operazione di lisciamento totale.

Valore predefinito: ""
maxSpeed: number

La velocità massima a cui la posizione attuale dovrebbe avvicinarsi alla posizione target.

Valore predefinito: ""
dt: number

Il tasso a cui l'operazione di lisciatura dovrebbe essere applicata.

Valore predefinito: ""

Restituzioni

La nuova posizione e velocità calcolata dall'operazione di lisciamento.

Eventi