TweenService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务

TweenService 用于创建 Tweens ,这会插入或中途修补间动画实例的属性。Tweens 可用于任何具有兼容属性类型的对象,包括:

TweenService:Create() , 主要建造者函数,需要 TweenInfo 关于青少年的规格,然后生成 Tween 对象,可以用来播放青补间动画。

请注意,Tweens 可以同时插入多个属性,但它们不能插入相同的属性。如果两个青少年尝试修改相同的属性,初始青少年将被取消并由最新的青少年覆盖。

代码示例

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

概要

方法

属性

方法

Create

这个生成器创建了一个新的 Tween 从三个参数中:要补间动画的对象、 TweenInfo 规格和包含要转换的属性和值的表。

propertyTable 参数需要是一个词典,其中键是属性的字符串名称(例如 Position , Transparency ,或 Color ),值是在渐变结束时属性目标。

使用此函数创建的 Tween 独特于指定为 instance 参数的对象。要将相同的青少年应用到另一个对象,请使用新对象再次调用此函数。

参数

instance: Instance

其属性将被拆分的 Instance

默认值:""
tweenInfo: TweenInfo

要使用的 TweenInfo

默认值:""
propertyTable: Dictionary

一个词典的属性和其目标值,以便进行中转。

默认值:""

返回

代码示例

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

返回使用给定的 alpha 值进行插值时的新 alpha 值, Enum.EasingStyle , 和 Enum.EasingDirection .提供的 alpha 值将被卡在 01 之间。

参数

alpha: number

介于 01 之间的插值值。

默认值:""
easingStyle: Enum.EasingStyle

使用的放松风格。

默认值:""
easingDirection: Enum.EasingDirection

使用的松动方向。

默认值:""

返回

从给定的减速风格和方向生成的新 alpha 值。

SmoothDamp

写入并联

返回允许向目标值平滑的 tuple,模拟出一个重要受抑制的弹簧。支持 Vector2 , Vector3 , CFrame , 和数字。

参数

current: Variant

当前位置。

默认值:""
target: Variant

目标位置。

默认值:""
velocity: Variant

当前位置应接近目标位置的初始速度。

默认值:""
smoothTime: number

总平滑操作应发场景的时间。

默认值:""
maxSpeed: number

当前位置应接近目标位置的最大速度。

默认值:""
dt: number

平滑操作应用的速率。

默认值:""

返回

从平滑操作中计算出的新位置和速度。

活动