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 Tweens những thuộc tính của các thực thể, hoặc giữa các thuộc tính của chúng. Tweens có thể được sử dụng trên bất kỳ đối tượng nào với các loại tính chất tương thích, bao gồm:
- boolean các thuộc tính các thuộc tính
TweenService:Create() , chức năng xây dựng chính, mất TweenInfo các thông số về tween và tạo ra đối tượng Tween có thể được sử dụng để chơi tween.
Lưu ý rằng Tweens có thể lồng ghép nhiều thuộc tính cùng một lúc, nhưng chúng không được lồng ghép cùng một thuộc tính.Nếu hai thiếu niên cố gắng thay đổi cùng một tính năng, tween ban đầu sẽ bị hủy và được viết lại bởi tween gần đây nhất.
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 mới Tween cho đối tượng có các thuộc tính cần được chuyển đổi, một TweenInfo , và một bảng từ điển các giá trị thuộc tính mục tiêu.
- GetValue(alpha : number,easingStyle : Enum.EasingStyle,easingDirection : Enum.EasingDirection):number
Tính toán một alpha mới cho một Enum.EasingStyle và Enum.EasingDirection .
- SmoothDamp(current : Variant,target : Variant,velocity : Variant,smoothTime : number,maxSpeed : number?,dt : number?):Tuple
Tính toán một giá trị mô phỏng một mùa xuân bị giảm nghiêm trọng.
Thuộc Tính
Phương Pháp
Create
Người xây dựng này tạo ra một đối tượng mới Tween từ ba tham số: đối tượng để tween, các thông số TweenInfo cụ thể, 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 mà các chìa khóa là tên chuỗi của thuộc tính ( ví dụ Position , Transparency , hoặc Color ), và các giá trị là mục tiêu thuộc tính ở cuối tween.
Các Tween được tạo bằng chức năng này là duy nhất đối với đối tượng được cung cấp là tham số instance.Để áp dụng tween giống cho một đối tượng khác, gọi lại chức năng này với đối tượng mới.
Tham Số
Một từ điển các thuộc tính và giá trị mục tiêu, để chuyển đổi.
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
Trả về một giá trị alpha mới để dò lặp bằng giá trị alpha đã cho, Enum.EasingStyle , và Enum.EasingDirection .Giá trị được cung cấp alpha sẽ bị giữa 0 và 1 .
Tham Số
Một giá trị interpolation giữa 0 và 1 .
Phong cách nới lỏng để sử dụng.
Hướng 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 được đưa ra.
SmoothDamp
Trả về một tuple cho phép làm mịn một giá trị đối với một mục tiêu, mô phỏng một mùa xuân bị giảm nghiêm trọng.Hỗ trợ Vector2 , Vector3 , CFrame , và số.
Tham Số
Vị trí hiện tại.
Vị trí mục tiêu.
Tốc độ ban đầu mà vị trí hiện tại nên tiếp cận vị trí mục tiêu.
Thời lượng mà hoạt động làm mịn tổng thể nên diễn địa điểm.
Tốc độ tối đa mà vị trí hiện tại nên tiếp cận vị trí mục tiêu.
Tỷ lệ mà hoạt động làm mịn nên được áp dụng.
Lợi Nhuận
Vị trí và tốc độ mới được tính toán từ hoạt động làm mịn.