TweenService

Mostrar obsoleto

*Este contenido se traduce usando la IA (Beta) y puede contener errores. Para ver esta página en inglés, haz clic en aquí.

No creable
Servicio

TweenService se usa para crear Tweens que interpolan o intermediación/interpolación de movimientolas propiedades de las instancias. Tweens se puede utilizar en cualquier objeto con tipos de propiedad compatibles, incluyendo:

TweenService:Create() , la función de constructor principal, toma TweenInfo especificaciones sobre el preadolescente y genera el objeto Tween que luego se puede usar para jugar al intermediación/interpolación de movimiento.

Tenga en cuenta que Tweens puede interpolar múltiples propiedades al mismo tiempo, pero no deben estar interpolando la misma propiedad.Si dos adolescentes intentan modificar la misma propiedad, el tween inicial se cancelará y se reemplazará por el intermediación/interpolación de movimientomás reciente.

Muestras de código

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

Resumen

Métodos

Propiedades

Métodos

Create

Este constructor crea un nuevo Tween de tres argumentos: el objeto para intermediación/interpolación de movimiento, las especificaciones TweenInfo específicas y una tabla que contiene las propiedades para transitar y los valores para transitar.

El parámetro propertyTable necesita ser un diccionario donde las claves son los nombres de las cadenas de la propiedad (por ejemplo Position , Transparency o Color ), y los valores son los objetivos de la propiedad al final del intermediación/interpolación de movimiento.

El Tween creado usando esta función es único para el objeto dado como el parámetro instance.Para aplicar el mismo preadolescente a otro objeto, llame nuevamente a esta función con el nuevo objeto.

Parámetros

instance: Instance

El Instance cuyas propiedades deben ser intermedias.

Valor predeterminado: ""
tweenInfo: TweenInfo

El TweenInfo para usar.

Valor predeterminado: ""
propertyTable: Dictionary

Un diccionario de propiedades y sus valores de destino, para ser intermedios.

Valor predeterminado: ""

Devuelve

Muestras de código

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

Devuelve un nuevo valor alfa para la interpolación usando el valor alfa dado, Enum.EasingStyle , y Enum.EasingDirection .El valor proporcionado alpha se clamará entre 0 y 1.

Parámetros

alpha: number

Un valor de interpolación entre 0 y 1 .

Valor predeterminado: ""
easingStyle: Enum.EasingStyle

El estilo de suavización para usar.

Valor predeterminado: ""
easingDirection: Enum.EasingDirection

La dirección de alivio para usar.

Valor predeterminado: ""

Devuelve

Un nuevo valor alfa generado desde el estilo y la dirección de alivio dados.

SmoothDamp

Escribir paralelo

Devuelve un túnel que permite suavizar un valor hacia un objetivo, simulando una resorte críticamente amortiguada.Soporta Vector2 , Vector3 , CFrame , y número.

Parámetros

current: Variant

La posición actual.

Valor predeterminado: ""
target: Variant

La posición objetivo.

Valor predeterminado: ""
velocity: Variant

La velocidad inicial en la que la posición actual debería acercarse a la posición objetivo.

Valor predeterminado: ""
smoothTime: number

La duración en la que debe llevarse a lugarla operación de suavización total.

Valor predeterminado: ""
maxSpeed: number

La velocidad máxima a la que la posición actual debería acercarse a la posición objetivo.

Valor predeterminado: ""
dt: number

La tasa a la que debe aplicarse la operación de suavización.

Valor predeterminado: ""

Devuelve

La nueva posición y velocidad calculada a partir de la operación de suavización.

Eventos