Lớp công cụ
AnimationTrack
*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.
Tóm Tắt
Thuộc Tính
Phương Pháp
AdjustSpeed(speed: number):() |
AdjustWeight(weight: number,fadeTime: number):() |
GetParameter(key: string):Variant |
GetTimeOfKeyframe(keyframeName: string):number |
SetParameter(key: string,value: Variant):() |
Sự Kiện
KeyframeReached(keyframeName: string):RBXScriptSignal |
Tài Liệu Tham Khảo API
Thuộc Tính
Animation
Mẫu mã
Lắng Nghe Các Hoạt Hình Mới
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
animator.AnimationPlayed:Connect(function(animationTrack)
local animationName = animationTrack.Animation.Name
print("Đang phát hoạt hình " .. animationName)
end)IsPlaying
Mẫu mã
AnimationTrack Đang Chạy
local function playOrAdjust(animationTrack, fadeTime, weight, speed)
if not animationTrack.IsPlaying then
animationTrack:Play(fadeTime, weight, speed)
else
animationTrack:AdjustSpeed(speed)
animationTrack:AdjustWeight(weight, fadeTime)
end
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playOrAdjust(animationTrack, 1, 0.6, 1)Length
Mẫu mã
Chơi Hoạt Hình Trong Một Thời Gian Cụ Thể
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)Looped
Mẫu mã
Vòng Lặp Hoạt Hình
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
local animationTrack = animator:LoadAnimation(animation)
animationTrack.Looped = false
task.wait(3)
animationTrack:Play()
task.wait(4)
animationTrack.Looped = true
animationTrack:Play()Chơi AnimationTrack trong một số vòng lặp
local function playForNumberLoops(animationTrack, number)
animationTrack.Looped = true
animationTrack:Play()
local numberOfLoops = 0
local connection = nil
connection = animationTrack.DidLoop:Connect(function()
numberOfLoops = numberOfLoops + 1
print("vòng lặp: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- điều quan trọng là phải ngắt kết nối khi không còn cần thiết
end
end)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playForNumberLoops(animationTrack, 5)Speed
Mẫu mã
Tốc độ Hoạt Hình
local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
ContentProvider:PreloadAsync({ animation })
local animationTrack = animator:LoadAnimation(animation)
local normalSpeedTime = animationTrack.Length / animationTrack.Speed
animationTrack:AdjustSpeed(3)
local fastSpeedTime = animationTrack.Length / animationTrack.Speed
print("Tại tốc độ bình thường, hoạt hình sẽ phát trong", normalSpeedTime, "giây")
print("Tại tốc độ 3x, hoạt hình sẽ phát trong", fastSpeedTime, "giây")Chơi Hoạt Hình Trong Một Thời Gian Cụ Thể
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)TimePosition
Mẫu mã
Đóng băng hoạt ảnh tại vị trí
function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying then
-- Phát hoạt ảnh nếu nó không đang phát
animationTrack:Play()
end
-- Đặt tốc độ thành 0 để đóng băng hoạt ảnh
animationTrack:AdjustSpeed(0)
-- Nhảy đến TimePosition mong muốn
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- Phát hoạt ảnh nếu nó không đang phát
animationTrack:Play()
end
-- Đặt tốc độ thành 0 để đóng băng hoạt ảnh
animationTrack:AdjustSpeed(0)
-- Nhảy đến TimePosition mong muốn
animationTrack.TimePosition = (percentagePosition / 100) * animationTrack.Length
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
freezeAnimationAtTime(animationTrack, 0.5)
freezeAnimationAtPercent(animationTrack, 50)WeightCurrent
Mẫu mã
Trọng số hiện tại và Trọng số mục tiêu
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation1 = Instance.new("Animation")
animation1.AnimationId = "rbxassetid://507770453"
local animation2 = Instance.new("Animation")
animation2.AnimationId = "rbxassetid://507771019"
task.wait(3) -- thời gian chờ tùy ý để cho phép nhân vật rơi vào vị trí
local animationTrack1 = animator:LoadAnimation(animation1)
local animationTrack2 = animator:LoadAnimation(animation2)
animationTrack1.Priority = Enum.AnimationPriority.Movement
animationTrack2.Priority = Enum.AnimationPriority.Action
animationTrack1:Play(0.1, 5, 1)
animationTrack2:Play(10, 3, 1)
local done = false
while not done and task.wait(0.1) do
if math.abs(animationTrack2.WeightCurrent - animationTrack2.WeightTarget) < 0.001 then
print("đã đến nơi")
done = true
end
endWeightTarget
Mẫu mã
Trọng số hiện tại và Trọng số mục tiêu
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation1 = Instance.new("Animation")
animation1.AnimationId = "rbxassetid://507770453"
local animation2 = Instance.new("Animation")
animation2.AnimationId = "rbxassetid://507771019"
task.wait(3) -- thời gian chờ tùy ý để cho phép nhân vật rơi vào vị trí
local animationTrack1 = animator:LoadAnimation(animation1)
local animationTrack2 = animator:LoadAnimation(animation2)
animationTrack1.Priority = Enum.AnimationPriority.Movement
animationTrack2.Priority = Enum.AnimationPriority.Action
animationTrack1:Play(0.1, 5, 1)
animationTrack2:Play(10, 3, 1)
local done = false
while not done and task.wait(0.1) do
if math.abs(animationTrack2.WeightCurrent - animationTrack2.WeightTarget) < 0.001 then
print("đã đến nơi")
done = true
end
endPhương Pháp
AdjustSpeed
Tham Số
| Giá Trị Mặc Định: 1 |
Lợi Nhuận
()
Mẫu mã
Chơi Hoạt Hình Trong Một Thời Gian Cụ Thể
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)Tốc độ Hoạt Hình
local ContentProvider = game:GetService("ContentProvider")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
while not localPlayer.Character do
task.wait()
end
local character = localPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507770453"
ContentProvider:PreloadAsync({ animation })
local animationTrack = animator:LoadAnimation(animation)
local normalSpeedTime = animationTrack.Length / animationTrack.Speed
animationTrack:AdjustSpeed(3)
local fastSpeedTime = animationTrack.Length / animationTrack.Speed
print("Tại tốc độ bình thường, hoạt hình sẽ phát trong", normalSpeedTime, "giây")
print("Tại tốc độ 3x, hoạt hình sẽ phát trong", fastSpeedTime, "giây")AdjustWeight
Lợi Nhuận
()
Mẫu mã
Thay Đổi Trọng Số AnimationTrack
local function changeWeight(animationTrack, weight, fadeTime)
animationTrack:AdjustWeight(weight, fadeTime)
local startTime = tick()
while math.abs(animationTrack.WeightCurrent - weight) > 0.001 do
task.wait()
end
print("Thời gian để thay đổi trọng số " .. tostring(tick() - startTime))
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
changeWeight(animationTrack, 0.6, 1)GetMarkerReachedSignal
Tham Số
Lợi Nhuận
Mẫu mã
Nghe các Đánh dấu Keyframe
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.Character:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- Tạo một thể hiện "Animation" mới
local kickAnimation = Instance.new("Animation")
-- Đặt "AnimationId" của nó thành ID tài sản hoạt ảnh tương ứng
kickAnimation.AnimationId = "rbxassetid://2515090838"
-- Tải hoạt ảnh lên humanoid
local kickAnimationTrack = humanoid:LoadAnimation(kickAnimation)
-- Phát track hoạt ảnh
kickAnimationTrack:Play()
-- Nếu một sự kiện có tên được định nghĩa cho hoạt ảnh, kết nối nó với "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)GetTimeOfKeyframe
Tham Số
Lợi Nhuận
Mẫu mã
Nhảy Đến Khung Hình
local function jumpToKeyframe(animationTrack, keyframeName)
local timePosition = animationTrack:GetTimeOfKeyframe(keyframeName)
if not animationTrack.IsPlaying then
animationTrack:Play()
end
animationTrack.TimePosition = timePosition
end
local ANIMATION_ID = 0
local KEYFRAME_NAME = "Test"
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. ANIMATION_ID
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
jumpToKeyframe(animationTrack, KEYFRAME_NAME)Play
Lợi Nhuận
()
Mẫu mã
Chơi Hoạt Hình Trong Một Thời Gian Cụ Thể
local function playAnimationForDuration(animationTrack, duration)
local speed = animationTrack.Length / duration
animationTrack:Play()
animationTrack:AdjustSpeed(speed)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765000"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playAnimationForDuration(animationTrack, 3)Đóng băng hoạt ảnh tại vị trí
function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying then
-- Phát hoạt ảnh nếu nó không đang phát
animationTrack:Play()
end
-- Đặt tốc độ thành 0 để đóng băng hoạt ảnh
animationTrack:AdjustSpeed(0)
-- Nhảy đến TimePosition mong muốn
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- Phát hoạt ảnh nếu nó không đang phát
animationTrack:Play()
end
-- Đặt tốc độ thành 0 để đóng băng hoạt ảnh
animationTrack:AdjustSpeed(0)
-- Nhảy đến TimePosition mong muốn
animationTrack.TimePosition = (percentagePosition / 100) * animationTrack.Length
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
freezeAnimationAtTime(animationTrack, 0.5)
freezeAnimationAtPercent(animationTrack, 50)SetParameter
Stop
Tham Số
| Giá Trị Mặc Định: 0.100000001 |
Lợi Nhuận
()
Mẫu mã
Dừng AnimationTrack
local function fadeOut(animationTrack, fadeTime)
animationTrack:Stop(fadeTime)
local startTime = tick()
while animationTrack.WeightCurrent > 0 do
task.wait()
end
local timeTaken = tick() - startTime
print("Thời gian cần để trọng số được đặt lại: " .. tostring(timeTaken))
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
fadeOut(animationTrack, 1)Sự Kiện
DidLoop
Mẫu mã
Chơi AnimationTrack trong một số vòng lặp
local function playForNumberLoops(animationTrack, number)
animationTrack.Looped = true
animationTrack:Play()
local numberOfLoops = 0
local connection = nil
connection = animationTrack.DidLoop:Connect(function()
numberOfLoops = numberOfLoops + 1
print("vòng lặp: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- điều quan trọng là phải ngắt kết nối khi không còn cần thiết
end
end)
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
playForNumberLoops(animationTrack, 5)Ended
Mẫu mã
Kết thúc AnimationTrack
local InsertService = game:GetService("InsertService")
local Players = game:GetService("Players")
-- Tạo một mô hình NPC để hoạt hình.
local npcModel = Players:CreateHumanoidModelFromUserIdAsync(129687796)
npcModel.Name = "JoeNPC"
npcModel.Parent = workspace
npcModel:MoveTo(Vector3.new(0, 15, 4))
local humanoid = npcModel:WaitForChild("Humanoid")
-- Tải một hoạt hình.
local animationModel = InsertService:LoadAsset(2510238627)
local animation = animationModel:FindFirstChildWhichIsA("Animation", true)
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
-- Kết nối với sự kiện Stopped. Điều này xảy ra khi hoạt hình dừng lại
-- theo cách riêng của nó, hoặc chúng ta gọi Stop một cách rõ ràng.
animationTrack.Stopped:Connect(function()
print("Hoạt hình đã dừng")
end)
-- Kết nối với sự kiện Ended. Điều này xảy ra khi hoạt hình hoàn toàn
-- kết thúc ảnh hưởng đến thế giới. Trong trường hợp này, nó sẽ xảy ra 3 giây
-- sau khi chúng ta gọi animationTrack:Stop vì chúng ta truyền vào một 3
-- giây fadeOut.
animationTrack.Ended:Connect(function()
print("Hoạt hình đã kết thúc")
animationTrack:Destroy()
end)
-- Chạy, cho nó một chút thời gian để phát, sau đó dừng lại.
print("Gọi Play")
animationTrack:Play()
task.wait(10)
print("Gọi Stop")
animationTrack:Stop(3)KeyframeReached
Stopped
Mẫu mã
AnimationTrack Dừng lại
local function yieldPlayAnimation(animationTrack, fadeTime, weight, speed)
animationTrack:Play(fadeTime, weight, speed)
animationTrack.Stopped:Wait()
print("Hoạt ảnh đã dừng lại")
end
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://507765644"
local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
yieldPlayAnimation(animationTrack, 1, 0.6, 1)