TweenService

顯示已棄用項目

*此內容是使用 AI(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

平滑操作應用的速率。

預設值:""

返回

由平滑操作計算出的新位置和速度。

活動