TweenBase

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

無法建立
無法瀏覽

抽象基礎類型為間接轉換處理器的子類;Tween 的父級。

概要

屬性

方法

  • Cancel():void

    停止播放,重設暫停變數。如果你然後呼叫 TweenBase:Play(),暫停變數的屬性會恢復到其目的地,但需要完整長度的動畫才能完成。

  • Pause():void

    停止播放暫停的補間動畫。不會重設其進度變量,因此如果您呼叫 TweenBase:Play(),殺手將從暫停的那一刻起恢復播放。

  • Play():void

    開始播放擺補間動畫。注意,如果擺動已經開始,呼叫 Play() 沒有效果,除非擺動已經完成或已停止 (或由 TweenBase:Cancel()TweenBase:Pause() 來停止)。

屬性

PlaybackState

唯讀
未複製
平行讀取

只閱取的屬性,顯示 Tween 動畫的當前階段。 查看 Enum.PlaybackState 以獲得每個狀態的說明。 使用 Tween:Play() 等功能。

範例程式碼

Tween PlaybackState

local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.Parent = workspace
local goal = {}
goal.Orientation = Vector3.new(0, 90, 0)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 2, true, 0.5)
local tween = TweenService:Create(part, tweenInfo, goal)
local function onPlaybackChanged()
print("Tween status has changed to:", tween.PlaybackState)
end
local playbackChanged = tween:GetPropertyChangedSignal("PlaybackState")
playbackChanged:Connect(onPlaybackChanged)
tween:Play()

方法

Cancel

void

停止播放 Tween 並重設 tween 變量。

只重設暫停變數,而不是暫停後變更的屬性。如果您在暫停過程中取消補間動畫,屬性將不會重設為原始值。與 TweenBase:Pause() 在暫停結束後恢復完畢的動畫不同,它需要完整暫停的時間才能完成動畫。


返回

void

範例程式碼

Tween Cancel

local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.Parent = workspace
local goal = {}
goal.Position = Vector3.new(0, 50, 0)
local tweenInfo = TweenInfo.new(5)
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()
task.wait(2.5)
tween:Cancel()
local playTick = tick()
tween:Play()
tween.Completed:Wait()
local timeTaken = tick() - playTick
print("Tween took " .. tostring(timeTaken) .. " secs to complete")
-- The tween will take 5 seconds to complete as the tween variables have been reset by tween:Cancel()

Pause

void

停止播放暫停的補間動畫。不會重設其進度變量,因此如果您呼叫 TweenBase:Play(),殺手將從暫停的那一刻起恢復播放。

如果要重設暫時的進補間動畫數,請使用 TweenBase:Cancel()

您只能暫停 PlaybackState 中的 Enum. PlaybackState.Playing ;在其他 Class.TweenBase.


返回

void

範例程式碼

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

Play

void

開始播放擺補間動畫。注意,如果擺動已經開始,呼叫 Play() 沒有效果,除非擺動已經完成或已停止 (或由 TweenBase:Cancel()TweenBase:Pause() 來停止)。

多個兒童可以在同一個對象上同時播放,但他們不能動畫相同的屬性。如果兩個兒童嘗試修改相同的屬性,初始的潮汐將被取消並由最近的潮汐覆蓋 (請參見範例)。


返回

void

範例程式碼

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

local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.Parent = game.Workspace
local tweenInfo = TweenInfo.new(5)
-- create two conflicting tweens (both trying to animate part.Position)
local tween1 = TweenService:Create(part, tweenInfo, { Position = Vector3.new(0, 10, 20) })
local tween2 = TweenService:Create(part, tweenInfo, { Position = Vector3.new(0, 30, 0) })
-- listen for their completion status
tween1.Completed:Connect(function(playbackState)
print("tween1: " .. tostring(playbackState))
end)
tween2.Completed:Connect(function(playbackState)
print("tween2: " .. tostring(playbackState))
end)
-- try to play them both
tween1:Play()
tween2:Play()

活動

Completed

發生時暫停播放或停止時使用 TweenBase:Cancel()

將潮汐的 Enum.PlaybackState 傳給任何連接的函數,以便提供潮汐結束的原因。注意,呼叫 TweenBase:Pause() 不會啟動 Completed 事件。

參數

playbackState: Enum.PlaybackState

完成時的 Enum.PlaybackState


範例程式碼

Tween Completed

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local SLOW_DURATION = 10
local function slowCharacter(humanoid)
local goal = {}
goal.WalkSpeed = 0
local tweenInfo = TweenInfo.new(SLOW_DURATION)
local tweenSpeed = TweenService:Create(humanoid, tweenInfo, goal)
tweenSpeed:Play()
return tweenSpeed
end
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local initialSpeed = humanoid.WalkSpeed
local tweenSpeed = slowCharacter(humanoid)
tweenSpeed.Completed:Wait()
humanoid.WalkSpeed = initialSpeed
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Tween Conflict

local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.Parent = game.Workspace
local tweenInfo = TweenInfo.new(5)
-- create two conflicting tweens (both trying to animate part.Position)
local tween1 = TweenService:Create(part, tweenInfo, { Position = Vector3.new(0, 10, 20) })
local tween2 = TweenService:Create(part, tweenInfo, { Position = Vector3.new(0, 30, 0) })
-- listen for their completion status
tween1.Completed:Connect(function(playbackState)
print("tween1: " .. tostring(playbackState))
end)
tween2.Completed:Connect(function(playbackState)
print("tween2: " .. tostring(playbackState))
end)
-- try to play them both
tween1:Play()
tween2:Play()
Verifying a Tween has Completed

local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Position = Vector3.new(0, 50, 0)
part.Anchored = true
part.Parent = workspace
local goal = {}
goal.Position = Vector3.new(0, 0, 0)
local tweenInfo = TweenInfo.new(3)
local tween = TweenService:Create(part, tweenInfo, goal)
local function onTweenCompleted(playbackState)
if playbackState == Enum.PlaybackState.Completed then
local explosion = Instance.new("Explosion")
explosion.Position = part.Position
explosion.Parent = workspace
part:Destroy()
task.delay(2, function()
if explosion then
explosion:Destroy()
end
end)
end
end
tween.Completed:Connect(onTweenCompleted)
tween:Play()