TweenService
*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.
TweenService được sử dụng để tạo ra Tweens which interpolate, or tween, the properties of instances. Tweens có thể được sử dụng trên bất kỳ đối tượng nào với các loại dữ liệu hợp nhất, bao gồm:
TweenService:Create() , chức năng chính của người xây dựng, nhận TweenInfo các thông số cụ thể về tween và tạo ra đối tượng Tween mà sau đó có thể được sử dụng để chơi tween.
Lưu ý rằng Tweens có thể xử lý nhiều thuộc tính cùng một lúc, nhưng chúng không thể xử lý cùng một thuộc tính. Nếu hai thiếu niên cố gắng điều chỉnh cùng một thuộc tính, một thiếu niên ban đầu sẽ bị huỷ và đổi bằng thuộ
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 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()
Tóm Tắt
Phương Pháp
Tạo một new Tween được cho là đối tượng có độ tơi tối đa cần được tơi tối, một TweenInfo và một bảng từ ngữ các giá trị ngôn ngữ mục tiêu.
- GetValue(alpha : number,easingStyle : Enum.EasingStyle,easingDirection : Enum.EasingDirection):number
Tính toán một alpha mới được cung cấp một Enum.EasingStyle và một Enum.EasingDirection .
- SmoothDamp(current : Variant,target : Variant,velocity : Variant,smoothTime : number,maxSpeed : number?,dt : number?):Tuple
Thuộc Tính
Phương Pháp
Create
Người xây dựng Create() tạo một Tween mới từ ba biến: đối tượng để tween, các đặc tả TweenInfo và một bảng chứa các thuộc tính để tween và giá trị để tween.
Tham số propertyTable cần phải là một từ điển nơi các chìa khóa là tên dòng của tính (như Position , Transparency hoặc 1> Color1> ) và giá trị là các mục tiêu tính ở cuối tween.
Các Tween được tạo bằng cách sử dụng hàm này là độc nhất với đối tượng được cung cấp như một đối tượng instance . Để áp dụng cùng một tween vào một đối tượng khác, hãy gọi hàm này một lần nữa với đối tượng mới.
Tham Số
Một từ điển các tính chất và giá trị mục tiêu của chúng, để được tùy chỉnh.
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 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
Đảm bảo một giá trị alpaca mới cho interpolating bằng giá trị alpaca được cung cấp, Enum.EasingStyle và Enum.EasingDirection . Giá trị alpaca được cung cấp sẽ được kẻ giữa alpha và 1> 11> .
Tham Số
Một giá trị biến giữa 0 và 1 .
Kiểu dễ dàng để sử dụng.
Hướng dẫn dễ dàng sử dụng.
Lợi Nhuận
Một giá trị alpha mới được tạo từ kiểu dễ dàng và hướng từ đó.