TweenBase
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Lớp cơ sở trừu tượng cho các xử lý interpolation giữa; lớp cha của Tween .
Tóm Tắt
Thuộc Tính
Tính năng chỉ đọc hiển thị trạng thái hiện tại cho hiệu ứng độnghình Tween .
Phương Pháp
Tạm dừng phát lại và đặt lại các biến tween.Nếu bạn sau đó gọi TweenBase:Play(), các thuộc tính của tween tiếp tục lượt truyền đến điểm đến của chúng, nhưng lấy toàn bộ chiều dài của hoạt hình để làm như vậy.
Dừng phát lại của tween.Không đặt lại các biến tiến trình của nó, có nghĩa là nếu bạn gọi TweenBase:Play(), tween sẽ tiếp tục phát lại từ lúc nó bị tạm dừng.
Bắt đầu phát lại của một tween.Lưu ý rằng nếu phát lại đã bắt đầu, gọi Play() không có hiệu lực trừ khi tween đã kết thúc hoặc bị ngừng (bởi TweenBase:Cancel() hoặc TweenBase:Pause() ).
Sự Kiện
Bắt lửa khi tween kết thúc chơi hoặc khi dừng lại với TweenBase:Cancel() .
Thuộc Tính
PlaybackState
Tính năng chỉ đọc hiển thị giai đoạn hiện tại cho hiệu ứng độnghình Tween .Xem Enum.PlaybackState để có mô tả về từng trạng thái.Thay đổi sử dụng chức năng như Tween:Play() .
Mẫu mã
In this example a part is rotated by a Tween back and forth several times. The TweenInfo in this case is configured to make the tween repeat twice after the first playback and pause between each playback. A function is connected to when the tween's PlaybackState changes. When run, this function will fire whenever the tween starts, pauses between playback, and ends.
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()
Phương Pháp
Cancel
Dừng lại phát lại của một Tween và đặt lại các biến tween.
Chỉ đặt lại các biến tween, không phải các thuộc tính được thay đổi bởi tweenNếu bạn hủy một tween giữa chừng trong hoạt hiệu ứng độngcủa nó, các thuộc tính không được đặt lại về giá trị ban đầu của chúng.Khác với TweenBase:Pause() trong đó một khi được khởi động lại, nó sẽ mất toàn bộ thời gian của tween để hoàn thành hoạt hiệu ứng động.
Lợi Nhuận
Mẫu mã
This sample demonstrates the impact of cancelling a tween.
A part is instanced in the Workspace and a tween is set up to move it along the Y axis. Mid way through the tween, it is cancelled. It can be observed here that the part does not return to its original position, but when it is resumed it takes the full length of the tween (5 seconds) to complete.
This is the key difference TweenBase:Pause() and TweenBase: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
Dừng phát lại của tween.Không đặt lại các biến tiến trình của nó, có nghĩa là nếu bạn gọi TweenBase:Play(), tween sẽ tiếp tục phát lại từ lúc nó bị tạm dừng.
Nếu bạn muốn đặt lại các biến tiến trình của tween, hãy sử dụng TweenBase:Cancel().
Bạn chỉ có thể tạm dừng thiếu niên ở trong PlaybackState của Enum. PlaybackState.Playing ; thiếu niên ở các tiểu bang khác sẽ không tạm dừng.Nếu một thiếu niên ở trong một khác như (như kết quả của việc nó lớn hơn 0), cố gắng tạm dừng thiếu niên sẽ thất bại và thiếu niên sẽ chơi theo thời gian trì hoãn được định trước.
Lợi Nhuận
Mẫu mã
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()
Play
Bắt đầu phát lại của một tween.Lưu ý rằng nếu phát lại đã bắt đầu, gọi Play() không có hiệu lực trừ khi tween đã kết thúc hoặc bị ngừng (bởi TweenBase:Cancel() hoặc TweenBase:Pause() ).
Nhiều thiếu niên có thể được chơi trên cùng một đối tượng cùng một lúc, nhưng chúng không được hoạt hình cùng một tính năng.Nếu hai thiếu niên cố gắng thay đổi cùng một tính năng, tween ban đầu bị hủy và được viết lại bởi tween gần đây nhất (xem ví dụ).
Lợi Nhuận
Mẫu mã
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 a demonstration of tween conflict. A part is instanced in the Workspace, and two tweens are created that attempt to move the part in conflicting directions.
When both tweens are played, the first tween is cancelled and overwritten by the second tween. This can be seen as the part moves along the Y axis as opposed to the Z axis.
To further demonstrate this, connections have been made for both tweens to the Tween.Completed event. Upon playing the tweens, the following is printed.
tween1: Enum.PlaybackState.Cancelled tween2: Enum.PlaybackState.Completed
These prints show that the first tween was cancelled (firing the Completed event) immediately upon the second tween being played. The second tween then went on to play until completion.
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()
Sự Kiện
Completed
Bắt lửa khi tween kết thúc chơi hoặc khi dừng lại với TweenBase:Cancel() .
Chuyển Enum.PlaybackState của tween cho bất kỳ chức năng kết nối nào để cung cấp một lời chỉ cho thấy tại sao tween kết thúc.Lưu ý rằng gọi TweenBase:Pause() không kích hoạt sự kiện Completed.
Tham Số
The Enum.PlaybackState của tween khi hoàn thành.
Mẫu mã
In this example the walkspeed of any player joining the game will be slowed to 0 over 10 seconds using a Tween. The Completed event is used to reset the walkspeed after the Tween has finished playing.
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)
This code sample includes a demonstration of tween conflict. A part is instanced in the Workspace, and two tweens are created that attempt to move the part in conflicting directions.
When both tweens are played, the first tween is cancelled and overwritten by the second tween. This can be seen as the part moves along the Y axis as opposed to the Z axis.
To further demonstrate this, connections have been made for both tweens to the Tween.Completed event. Upon playing the tweens, the following is printed.
tween1: Enum.PlaybackState.Cancelled tween2: Enum.PlaybackState.Completed
These prints show that the first tween was cancelled (firing the Completed event) immediately upon the second tween being played. The second tween then went on to play until completion.
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()
This code sample includes an example of how Tween.Completed can be used to determine if a Tween has been successfully completed, or cancelled.
In this case a part is instanced and tweened towards 0, 0, 0. Once the tween has completed, if the final PlaybackState is Completed then the part will explode. Were the tween to be cancelled prior to completion, the explosion would not be created.
This method can be used to link tweens to other effects, or even chain several tweens to play after each other.
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()