TweenService

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง
บริการ

TweenService ใช้เพื่อสร้าง Tweens ซึ่งเป็นการเรียนรู้หรือเรียนรู้สมบัติของตัวอย่าง Tweens สามารถใช้กับวัตถุที่มีประเภทสมบัติที่เข้ากัน

TweenService:Create() รหัสผู้สร้างหลัก ใช้ TweenInfo ข้อมูลเกี่ยวกับ tween และสร้างวัตถุ Tween ซึ่งสามารถใช้ได้เพื่อเล่น 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

ผู้สร้าง Create() สร้าง Class.Tween ใหม่จากสามตัวอักษร: ตัวอักษรที่จะทวีคูณ ข้อมูล Tween และตารางที่มีสมบัติสินค้าที่จะทวีคูณและค่าที่จะทวีคูณ

ตัวแปร propertyTable ต้องเป็นพจนารูปแบบที่มีคีย์เป็นชื่อสตริงของคุณสมบัติ (เช่น Position , Transparency หรือ 1> Color1> ) และมีค่าเป็นเป้าหมายคุณสมบัติในตอนท้ายข

Class.Tween ที่สร้างโดยใช้คุณสมบัตินี้ไม่เหมือนกับวัตถุที่ให้เป็นตัวประกอบ instance เพื่อใช้กับวัตถุอื่น โปรดเรียกใช้คุณสมบัตินี้อีกครั้งด้วยวัตถุใหม่

พารามิเตอร์

instance: Instance

Class.Instance ซึ่งมีสมบัติสกุล Class.Instance

tweenInfo: TweenInfo

Datatype.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

กลับมาให้ค่าอัลฟาใหม่สำหรับการส่งผ่านโดยใช้ค่าอัลฟาที่ให้ไว้, Enum.EasingStyle และ Enum.EasingDirection ค่า alpha ที่จะถูกปรับแต่งระหว่าง 1> 0

พารามิเตอร์

alpha: number

ค่าการแปลงระหว่าง 0 และ 1

easingStyle: Enum.EasingStyle

สไตล์การปลดล็อคใช้งาน

easingDirection: Enum.EasingDirection

ทิศทางการประหยัดใช้งาน


ส่งค่ากลับ

มีค่า alpha ใหม่ที่สร้างจากสไตล์และทิศทางที่ให้ไว้

SmoothDamp

พารามิเตอร์

current: Variant
target: Variant
velocity: Variant
smoothTime: number
maxSpeed: number
dt: number

ส่งค่ากลับ

อีเวนต์