AnimationTrack

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建

控制在 AnimationController 上播放动画。这个对象不能创建,它被返回 Animator:LoadAnimation() 方法。

概要

属性

方法

活动

属性

Animation

只读
未复制
读取并联

用于创建此 AnimationTrack 的对象。要创建一个 AnimationTrack ,您必须先加载一个 1> Class.Animation1> 对象到一个 4> Class.Animator:LoadAnimation()4> 使用方法。

代码示例

The following code sample prints the name of an animation whenever an AnimationTrack plays on a Humanoid. This script can be placed in a model with a Humanoid child.

Listen For New Animations

local humanoid = script.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
animator.AnimationPlayed:Connect(function(animationTrack)
local animationName = animationTrack.Animation.Name
print("Animation playing " .. animationName)
end)

IsPlaying

只读
未复制
读取并联

一个只读属性,当 AnimationTrack 正在播放时返回 true。

此属性可以被开发人员用来检查是否有动画正在播放,然后再播放它(因为这会导致它重新启动)。如果开发人员希望获得所有正在播放的 AnimationTracks 在一个 HumanoidAnimationController 上,他们应该使用 2>Class.Humanoid

代码示例

This code sample includes a simple function that will play an AnimationTrack if it is not playing, or otherwise adjust its speed and weight to match the new parameters given.

AnimationTrack IsPlaying

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

只读
未复制
读取并联

一个只读属性,返回 AnimationTrack 的长度(以秒为单位)。 这将返回 0 直到动画已完全加载,因此可能不会立即可用。

AnimationTrack.Speed 的一个 AnimationTrack 与 1 相等时,动画需要 AnimationTrack.Length (在秒内) 才能完成。

代码示例

The following function will play an AnimationTrack for a specific duration. This is done by changing the speed of the animation to the length of the animation divided by the desired playback duration. This could be used in situations where a developer wants to play a standard animation for different duration (for example, recharging different abilities).

Playing Animation for a Specific Duration

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

读取并联

此属性设置是否在完成后重复动画。如果在玩结果时更改,将在动画完成后生效。

Class.AnimationTrack 的循环属性默认为在动画编辑器中设置的方式。 但是,这个属性可以更改,允许在游戏运行时控制 AnimationTrack 。 循环还正确处理反向动画 (负 AnimationTrack.Speed )。 首个关卡框达到后,它将在最后关键帧重新启动。

这个属性允许开发人员有一个循环和非循环的同一动画的变体,无需要将两个版本上传到 Roblox。

代码示例

The animation in this example normally loops. After the player and the animation are loaded the animation is played in a non-looped fashion then in a looped fashion.

Animation Looping

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()

The function in this code sample will play an AnimationTrack on a loop, for a specific number of loops, before stopping the animation.

In some cases the developer may want to stop a looped animation after a certain number of loops have completed, rather than after a certain amount of time. This is where the DidLoop event can be used.

Play AnimationTrack for a Number of Loops

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("loop: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- it's important to disconnect connections when they are no longer needed
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)
读取并联

此属性设置了一个 AnimationTrack 的优先级。根据此设置,在多个动画同时播放时,将向此属性看来以确定哪个 Keyframe 将在一个 Poses 上播放。

Class.AnimationTrack 的优先级属性默认为 Studio 的 动画编辑器 设置和发布。它使用 Enum.AnimationPriority ,这有 7 个优先级级别:

  1. Action4 (最高优先级)
  2. 动作 3
  3. 动作 2
  4. 行动
  5. 移动
  6. 空闲
  7. 核心 (最低优先级)

通过编辑器或此属性设置动画优先级,允许多个动画在不会发生碰撞的情况下播放。如果两个播放动画通过同一种方式将目标移动到同一条腿上,最高优先级的 AnimationTrack 将显示。如果两个动画都有相同的优先级,轨道的重量将用于组合动画。

此属性还允许开发人员在不同的优先级下播放相同的动画,无需要将额外版本上传到 Roblox。

Speed

只读
未复制
读取并联

Class.AnimationTrack 的速度是一个只读属性,它给当前播放速度的 AnimationTrack 的当前播放速度。 它的默认值为 1 。当速度等于 1 时,时间完成动画的时间会相等于 AnimationTrack.Length (在秒为单位)。

如果速度调整, then the actual time it will take a track to play 可以通过将长度乘以速度计算一个轨道播放的实际时间。速度是一个无量的单位。

速度可以用来将动画的长度连接到不同的游戏事件(例如重新充能一个能力),无需上传不同变体的相同动画。

此属性仅供读取,您可以使用 AnimationTrack:AdjustSpeed() 来更改它。

代码示例

In this example a player and an animation is loaded. The Length of an AnimationTrack determines how long the track would take to play if the speed is at 1. If the speed is adjusted, then the actual time it will take a track to play can be computed by dividing the length by the speed.

Animation Speed

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("At normal speed the animation will play for", normalSpeedTime, "seconds")
print("At 3x speed the animation will play for", fastSpeedTime, "seconds")

The following function will play an AnimationTrack for a specific duration. This is done by changing the speed of the animation to the length of the animation divided by the desired playback duration. This could be used in situations where a developer wants to play a standard animation for different duration (for example, recharging different abilities).

Playing Animation for a Specific Duration

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

未复制
读取并联

返回一个 AnimationTrack 的位置在几秒内,通过播放其源动画。可以设置它来在动画中跳到特定时刻。

时间位置可以设置为在动画中到达特定点,但 AnimationTrack 必须正在播放才能使用它。它还可以与 AnimationTrack:AdjustSpeed() 组合使用,以将动画设置为所需位置 (通过将速度设置为 0)。

代码示例

The following code sample includes two functions that demonstrate how AdjustSpeed and TimePosition can be used to freeze an animation at a particular point.

The first function freezes an animation at a particular point in time (defined in seconds). The second freezes at it at a percentage (between 0 or 100) by multiplying the percentage by the track length.

As TimePosition can not be used when an AnimationTrack is not playing, the functions check to make sure the animation is playing before proceeding.

Freeze Animation at Position

function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired TimePosition
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired TimePosition
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

只读
未复制
读取并联

当重量设置在 AnimationTrack 中时,它不会立即更改,而是从 WeightCurrent 移动到 AnimationTrack.WeightTarget 。 时间它所需的时间由渐变时间参数给出,当动画播放时,或重量调整。

重量目标可以检查 against AnimationTrack.WeightTarget 来确定目标重量是否已达到所需的重量。注意,这些值不应与等级操作器检查,因为这两个值都是漂浮。要确定 WeightCurrent 达到目标重量,请确保距离这些值足够小 (请参阅下面的代码示例)。

动画重量系统用于确定如何在相同的优先级下播放 AnimationTracks 的动画是否会被混合在一起。默认重量是一个,在没有重量的情况下

代码示例

This code sample loads two animations onto the local player's Humanoid and demonstrates how the fadeTime paramater in AnimationTrack.Play determines how long it takes for an AnimationTrack's WeightCurrent to reach it's WeightTarget.

As WeightCurrent and WeightTarget are floats the == operator cannot be used to compare, instead it is more appropriate to check that the difference between them is sufficiently small to assume the weight fade has completed.

WeightCurrent and WeightTarget

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) -- arbitrary wait time to allow the character to fall into place
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("got there")
done = true
end
end

WeightTarget

只读
未复制
读取并联

AnimationTrack.WeightTarget 是一个只读的属性,它将当前重量的 AnimationTrack 的当前重量设置为 1 。它的默认值为

重量目标可以检查 against AnimationTrack.WeightTarget 来确定目标重量是否已达到所需的重量。注意,这些值不应与等级操作器检查,因为这两个值都是漂浮。要确定 WeightCurrent 达到目标重量,请确保距离这些值足够小 (请参阅下面的代码示例)。

动画重量系统用于确定如何在相同的优先级下播放 AnimationTracks 的动画是否会被混合在一起。默认重量是一个,在没有重量的情况下

代码示例

This code sample loads two animations onto the local player's Humanoid and demonstrates how the fadeTime paramater in AnimationTrack.Play determines how long it takes for an AnimationTrack's WeightCurrent to reach it's WeightTarget.

As WeightCurrent and WeightTarget are floats the == operator cannot be used to compare, instead it is more appropriate to check that the difference between them is sufficiently small to assume the weight fade has completed.

WeightCurrent and WeightTarget

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) -- arbitrary wait time to allow the character to fall into place
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("got there")
done = true
end
end

方法

AdjustSpeed

void

此函数改变了动画的 AnimationTrack.Speed 。一个正向的速度会将动画向前播放,一个负向的速度会将动画向后播放,而 0 将它暂停。

AnimationTrack:Play() 中,初始速度设置为参数。但是,跟踪的速度在播放时可以使用 AdjustSpeed 进行调整。当速度等于 1 时,跟踪的完成时间等同于 AnimationTrack.Length (以秒为单位) (在秒内)。

调整时,可以根据速度将实际播放轨道的时间计算出来。速度是一个无量的单位。

速度可以用来将动画的长度连接到不同的游戏玩法事件(例如重新充能一个能力),无需上传不同变体的相同动画。

参数

speed: number

播放速度将更改为所选动画。

默认值:1

返回

void

代码示例

The following function will play an AnimationTrack for a specific duration. This is done by changing the speed of the animation to the length of the animation divided by the desired playback duration. This could be used in situations where a developer wants to play a standard animation for different duration (for example, recharging different abilities).

Playing Animation for a Specific Duration

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)

In this example a player and an animation is loaded. The Length of an AnimationTrack determines how long the track would take to play if the speed is at 1. If the speed is adjusted, then the actual time it will take a track to play can be computed by dividing the length by the speed.

Animation Speed

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("At normal speed the animation will play for", normalSpeedTime, "seconds")
print("At 3x speed the animation will play for", fastSpeedTime, "seconds")

AdjustWeight

void

更改动画的重量,并且可选的渐变时间参数决定它需要多久才能达到AnimationTrack.WeightCurrent

当重量设置在 AnimationTrack 中时,它不会立即更改,而是从 WeightCurrent 移动到 AnimationTrack.WeightTarget 。 时间它所需的时间由渐变时间参数给出,当动画播放时,或重量调整。

重量目标可以检查 against AnimationTrack.WeightTarget 来确定目标重量是否已达到所需的重量。注意,这些值不应与等级操作器检查,因为这两个值都是漂浮。要确定 WeightCurrent 达到目标重量,请确保距离这些值足够小 (请参阅下面的代码示例)。

动画权重系统用于确定 AnimationTracks 在同一优先级下播放是否会混合在一起。默认重量是一个,无

参数

weight: number

动画的重量要更改为。

默认值:1
fadeTime: number

动画在旧重量和新重量之间渐渐消失的时间。

默认值:0.100000001

返回

void

代码示例

This code sample includes a function that changes the weight of an AnimationTrack and yields until the weight has changed to the new target weight.

The purpose of this sample is to demonstrate how the fadeTime parameter of AnimationTrack.AdjustWeight works. In most cases, if a developer wishes to yield over the fadeTime it is recommended they use wait(fadeTime).

AnimationTrack Change Weight

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("Time taken to change weight " .. 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

此函数返回一个 eventAnimationTrack.KeyframeReached 事件类似,除了它只会在指定的 KeyframeMarker 被击中时才会发生,差异允许更好地控制事件何时发触发。

要了解有关使用此函数的更多信息,请参阅动画事件在动画编辑器文章中。

更多关于 Keyframes

Keyframe 名称可以在 Roblox 动画编辑器 中设置动画时设置或编辑动画。 但是,它们不能由 Script 在现有动画上设置或编辑动画之前设置。

Keyframe 名称不需要独特。例如,如果 Animation 有三个名为“EmitParticles”的关键框,此函数将在每个达到这些关键框之一时触发连接事件。

还请参阅:

参数

name: string

信号的名称为 KeyFrameMarker


返回

当动画到达创建的 KeyFrameMarker 时,信号已创建并发射。

代码示例

This LocalScript code waits for the local player's Humanoid object to load, then it creates a new Animation instance with the proper Animation.AnimationId. The animation is then loaded onto the humanoid, creating an AnimationTrack, and the track is played with AnimationTrack:Play(). Following that, the AnimationTrack:GetMarkerReachedSignal() function detects when the "KickEnd" marker is hit.

Listening to Keyframe Markers

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.Character:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- Create new "Animation" instance
local kickAnimation = Instance.new("Animation")
-- Set its "AnimationId" to the corresponding animation asset ID
kickAnimation.AnimationId = "rbxassetid://2515090838"
-- Load animation onto the humanoid
local kickAnimationTrack = humanoid:LoadAnimation(kickAnimation)
-- Play animation track
kickAnimationTrack:Play()
-- If a named event was defined for the animation, connect it to "GetMarkerReachedSignal()"
kickAnimationTrack:GetMarkerReachedSignal("KickEnd"):Connect(function(paramString)
print(paramString)
end)

GetTimeOfKeyframe

AnimationTrack 中返回给定名称的第一个 Keyframes 的时间位置。如果多个 1> Class.Keyframe|Keyframes1> 共享相同的名称,它将返回动画中最早的一个。

如果使用了一个无效的 keyframe 名称(例如,不存在的)或基础 Animation 还未加载,这个函数将返回一个错误。为了解决此问题,请确保仅使用正确的 keyframe 名称,并且在调用此函数之前动画已加载。

要检查动画是否已加载,请验证 AnimationTrack.Length 是否大于零。

参数

keyframeName: string

Keyframe 相关的名称。


返回

时间,以秒为单位,在正常播放速度下发生。

代码示例

This sample includes a function that will jump to the first keyframe of a specified name in an AnimationTrack.

As AnimationTrack.TimePosition cannot be set while the animation is not playing the function first checks to see if the animation is playing.

This sample will only work once an Animation has loaded.

Jump To Keyframe

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

void

AnimationTrack:Play() 被调用时,该轨道的动画将开始播放,重量将从 0 提升到指定的重量 (默认为 1) 在指定的渐变时间 (默认为 0.1) 过。

Class.AnimationTrack 的速度将由速度参数确定 (默认为 1)。当速度等于 1 时,跟踪的完成时间会相等于跟踪的 AnimationTrack.Length 属性。例如,2 的速度会使跟踪播放两倍更快。

动画开始后,您可以使用 AnimationTrack:AdjustWeight()AnimationTrack:AdjustSpeed() 方法来更改动画的重量和速度。

如果开发者想在特定时间使用 AnimationTrack.TimePosition 开始动画,那么在此之前播放动画是重要的。

参数

fadeTime: number

动画的重量应该在什么时候褪色。

默认值:0.100000001
weight: number

动画的重量将在以下位置播放。

默认值:1
speed: number

动画的播放速度。

默认值:1

返回

void

代码示例

The following function will play an AnimationTrack for a specific duration. This is done by changing the speed of the animation to the length of the animation divided by the desired playback duration. This could be used in situations where a developer wants to play a standard animation for different duration (for example, recharging different abilities).

Playing Animation for a Specific Duration

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)

The following code sample includes two functions that demonstrate how AdjustSpeed and TimePosition can be used to freeze an animation at a particular point.

The first function freezes an animation at a particular point in time (defined in seconds). The second freezes at it at a percentage (between 0 or 100) by multiplying the percentage by the track length.

As TimePosition can not be used when an AnimationTrack is not playing, the functions check to make sure the animation is playing before proceeding.

Freeze Animation at Position

function freezeAnimationAtTime(animationTrack, timePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired TimePosition
animationTrack.TimePosition = timePosition
end
function freezeAnimationAtPercent(animationTrack, percentagePosition)
if not animationTrack.IsPlaying then
-- Play the animation if it is not playing
animationTrack:Play()
end
-- Set the speed to 0 to freeze the animation
animationTrack:AdjustSpeed(0)
-- Jump to the desired TimePosition
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)

Stop

void

停止 AnimationTrack 。一旦调用 AnimationTrack 的播放,将会停止,重量的动画将向零移动在指定的时间长度上。

例如,如果使用了一个 fadeTime 为 2 秒的停止,它的重量将在 2 秒内完全结束,从而达到零。请注意,无论动画的初始重量如何,这都会是如此。

不建议使用 0 秒的渐变时间来尝试覆盖此效果并立即结束动画,因为这会导致 AnimationTrack 的姿势冻结。

参数

fadeTime: number

过度动画重量的时间,以秒为单位。

默认值:0.100000001

返回

void

代码示例

This code sample includes a function that stops an AnimationTrack with a specific fadeTime, and yields until the fade is completed and the weight of the AnimationTrack is equal to zero.

The purpose of this sample is to demonstrate how the fadeTime parameter of AnimationTrack.Stop works. In most cases, if a developer wishes to yield over the fadeTime it is recommended they use wait(fadeTime).

AnimationTrack Stop

local function fadeOut(animationTrack, fadeTime)
animationTrack:Stop(fadeTime)
local startTime = tick()
while animationTrack.WeightCurrent > 0 do
task.wait()
end
local timeTaken = tick() - startTime
print("Time taken for weight to reset: " .. 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)

活动

DidLoop

当有AnimationTrack 完成时,该事件会触发。在下次更新时。

当前可能还会在不循环动画轨道的正确端发射,但这种行为不应被视为可靠。


代码示例

The function in this code sample will play an AnimationTrack on a loop, for a specific number of loops, before stopping the animation.

In some cases the developer may want to stop a looped animation after a certain number of loops have completed, rather than after a certain amount of time. This is where the DidLoop event can be used.

Play AnimationTrack for a Number of Loops

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("loop: ", numberOfLoops)
if numberOfLoops >= number then
animationTrack:Stop()
connection:Disconnect() -- it's important to disconnect connections when they are no longer needed
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

AnimationTrack 完全完成移动世界上的任何东西时,动画已完成。动画已完成播放,“渐变”已完成,主题在中立姿势。

您可以使用此来在动画跟踪的主题在中立姿势中回到时执行此操作,这不会受到 AnimationTrack 或任何关联的连接的影响。


代码示例

The function in this code sample plays an animationTrack and yields until it has stopped and ended, printing at each step along the way.

AnimationTrack Ended

local InsertService = game:GetService("InsertService")
local Players = game:GetService("Players")
-- Create an NPC model to animate.
local npcModel = Players:CreateHumanoidModelFromUserId(129687796)
npcModel.Name = "JoeNPC"
npcModel.Parent = workspace
npcModel:MoveTo(Vector3.new(0, 15, 4))
local humanoid = npcModel:WaitForChild("Humanoid")
-- Load an animation.
local animationModel = InsertService:LoadAsset(2510238627)
local animation = animationModel:FindFirstChildWhichIsA("Animation", true)
local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)
-- Connect to Stopped event. This fires when animation stops of
-- it's own accord, or we explicitly call Stop.
animationTrack.Stopped:Connect(function()
print("Animation stopped")
end)
-- Connect to Ended event. This fires when when animation is completely
-- finished affecting the world. In this case it will fire 3 seconds
-- after we call animationTrack:Stop because we pass in a 3
-- second fadeOut.
animationTrack.Ended:Connect(function()
print("Animation ended")
animationTrack:Destroy()
end)
-- Run, give it a bit to play, then stop.
print("Calling Play")
animationTrack:Play()
task.wait(10)
print("Calling Stop")
animationTrack:Stop(3)

KeyframeReached

每当 AnimationTrack 的播放达到一个 Keyframe ,它不会有默认名称 - "Keyframe"。

此事件允许开发人员在动画中在预定点上运行代码(由 Keyframe 命名)。这允许 Roblox 动画的默认功能在添加 SoundsParticleEffects 到不同的点在动画中扩展。

Keyframe 名称不需要独特。例如,如果动画有三个名为“粒子”的关键框,键框达到事件将触发每次一个关键框达到。

Keyframe 名称可以在 Roblox 动画编辑器中设置或编辑动画时设置。但是,在播放之前,Script 不能设置它。

参数

keyframeName: string

Class.Keyframe 的名称已达到。


Stopped

发射 AnimationTrack 完成播放时。

这个事件有多个使用。它可以用于等待一个 AnimationTrack 停止运行,然后继续(例如,如果您连续播放了多个动画来播放之后)。它还可以用于清理任何 Instances 在动画播放时创建。


代码示例

The function in this code sample will play an animationTrack and yield until it has stopped, before printing.

AnimationTrack Stopped

local function yieldPlayAnimation(animationTrack, fadeTime, weight, speed)
animationTrack:Play(fadeTime, weight, speed)
animationTrack.Stopped:Wait()
print("Animation has stopped")
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)