TweenService
*Este conteúdo é traduzido por IA (Beta) e pode conter erros. Para ver a página em inglês, clique aqui.
TweenService se usa para crear Tweens que interpola, o intermediación/interpolación de movimiento, las propiedades de las instancias. Tweens se puede usar en cualquier objeto con tipos de propiedad compatibles, incluido:
TweenService:Create() , la función de constructor principal, toma TweenInfo especificaciones sobre el tween y genera el objeto Tween que se puede usar para jugar el intermediación/interpolación de movimiento.
Nota que Tweens puede interpolar múltiples propiedades al mismo tiempo, pero no deben estar interpolando la misma propiedad. Si dos tweens intentan modificar la misma propiedad, el tween inicial se cancelará y se escribirá por el intermediación/interpolación de movimientomás reciente.
Amostras 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.
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()
Resumo
Métodos
Crea una nueva Tween dado el objeto cuyas propiedades se tienen que tener en cuenta, un TweenInfo y un diccionario de valores de propiedad de objetivo.
- GetValue(alpha : number,easingStyle : Enum.EasingStyle,easingDirection : Enum.EasingDirection):number
Calcula un nuevo alfa dado un Enum.EasingStyle y un Enum.EasingDirection .
- SmoothDamp(current : Variant,target : Variant,velocity : Variant,smoothTime : number,maxSpeed : number?,dt : number?):Tuple
Propriedades
Métodos
Create
El constructor Create() crea un nuevo Tween a partir de tres argumentos: el objeto para tener, las especificaciones de TweenInfo y una tabla que contiene las propiedades para tener y valores para tener.
El parámetro propertyTable necesita ser una lista de diccionario en la que las llaves son los nombres de la propiedad (por ejemplo, Position, Transparency, o 1> Color1>), y los valores son los objetivos de propiedad en el final del intermediación/interpolación de movimiento.
El Class.Tween creado con esta función es único para el objeto dado como el parámetro instance. Para aplicar el mismo tween a otro objeto, llama a esta función nuevamente con el nuevo objeto.
Parâmetros
Un diccionario de propiedades y sus valores de destino, para ser tweened.
Devolução
Amostras 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.
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
Devuelve un nuevo valor alfa para interpolar utilizando el valor alfa proporcionado, Enum.EasingStyle y Enum.EasingDirection . El valor de alpha proporcionado se ajustará entre 1> 01> y 4> 14> .
Parâmetros
Un valor de interpolación entre 0 y 1 .
El estilo de fácil uso.
La dirección de fácil uso.
Devolução
Un nuevo valor de alfa generado desde el estilo de fácil acceso y la dirección proporcionados.