在 Animator 上控制动画的播放。此对象无法创建,而是由 Animator:LoadAnimation() 方法返回。
概要
属性
用于创建此 AnimationTrack 的 Animation 对象。
只读属性,当 AnimationTrack 播放时返回真值。
一个只读属性,返回 AnimationTrack 的长度 (以秒为单位)。直到动画完全加载为止,这将返回 0,因此可能不会立即可用。
设置动画在完成后是否重复。如果在游戏期间更改,结果动画完成后才会生效。
设置 AnimationTrack 的优先级。根据设置的内容,单次播放多个动画时,将查看此属性来确定哪个 Keyframe Poses 应该在另一个上播放。
一个 AnimationTrack 的速度是一个只读属性,可以提供当前播放速度的 AnimationTrack 。这有一个默认值 1。当速度等于 1 时,动画完成所需的时间等于 AnimationTrack.Length (秒)。
返回在 AnimationTrack 播放其源动画时的时间位置,该动画源于某个源。可以设置为将轨道跳到动画中的特定时刻。
只读属性,用于 AnimationTrack 的当前重量。它有默认值为 1。
只读属性,可阅读 AnimationTrack 的当前重量。
方法
更改动画的 AnimationTrack.Speed 。正向速度的值播放动画向前,负向速度的值播放向后,0 暂停它。
更改动画的重量,其中可选的 fadeTime 参数决定 AnimationTrack.WeightCurrent 到达 AnimationTrack.WeightTarget 的时间长度。
返回一个 event ,当特定的 KeyframeMarker 在 animation 中被击中时发射。
返回给定名称的第一个 Keyframe 的时间位置在 AnimationTrack 。
播放 AnimationTrack 。一旦调用,AnimationTrack 将与指定的渐变时间、重量和速度一起播放。
停止 AnimationTrack 。
活动
在前一个动画循环结束后的下一次更新中发生火灾,当 AnimationTrack 循环在下一个更新后出现时
当 AnimationTrack 完全完成移动世界上的任何东西时发生火灾动画已经播放完毕,“渐变”已经完成,主题处于中立姿态。
每次播放的 AnimationTrack 达到一个没有默认名称 - "键帧" - 的 Keyframe 时发生火焰。
当 AnimationTrack 结束播放时发生火焰。动画跟踪可能仍然会在动画“消失”时动画主题。要在动画跟踪完全完成移动世界上的任何东西时捕获,请使用 AnimationTrack.Ended 事件。
属性
Animation
用于创建此 AnimationTrack 的 Animation 对象。要创建一个 AnimationTrack ,您必须使用 Animation 方法将一个 Animator 对象加载到 Animator:LoadAnimation() 上。
代码示例
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.
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 播放时返回真值。
开发人员可以使用此属性来检查动画在播放之前是否已经播放(因为这会导致它重新启动)。如果开发者想要获得所有播放 AnimationTracks 在 Animator 或 Humanoid 上,他们应该使用 Animator:GetPlayingAnimationTracks()
代码示例
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.
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).
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
此属性设置动画在完成后是否会重复。如果在游戏时更改,结果将在动画完成后生效。
循环属性为 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.
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.
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)
Priority
该属性设置 AnimationTrack 的优先级。根据设置的内容,单次播放多个动画时,将查看此属性来确定哪个 Keyframe Poses 应该在另一个上播放。
优先级属性为 AnimationTrack 默认设置为从 Studio 的 动画编辑器 设置和发布的方式。它使用 Enum.AnimationPriority ,它有 7 个优先级:
- 行动4(最高优先级)
- 行动3
- 行动2
- 行动
- 移动
- 空闲
- 核心(最低优先级)
正确设置动画优先级,通过编辑器或通过此属性,可以让多个动画在不冲突的情况下播放。当两个正在播放的动画将目标指向以不同方式移动同一肢体时,最高优先级的 AnimationTrack 将显示。如果两个动画都有相同的优先级,轨道的重量将用于结合动画。
此属性还允许开发者在不同优先级下播放相同的动画,无需上传额外版本到 Roblox。
Speed
一个 AnimationTrack 的速度是一个只读属性,可以提供当前播放速度的 AnimationTrack 。这有一个默认值 1。当速度等于 1 时,动画完成所需的时间等于 AnimationTrack.Length (秒)。
如果速度被调整,那么实际播放所需的时间可以通过将长度除以速度来计算。速度是一种无单位数量。
速度可用于将动画长度与不同的游戏事件链接(例如重新充能一个能力),无需上传不同版本的相同动画。
该属性只读,您可以使用 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.
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).
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: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.
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 设置时,它不会立即更改,但会从重量当前移动到 AnimationTrack.WeightTarget 。执行此操作所需的时间由在动画播放或重量调整时提供的 fadeTime 参数决定。
重量当前可以与 AnimationTrack.WeightTarget 对比,以检查是否达到了所需重量。请注意,这些值不应与 == 运营符相等进行检查,因为这两个值都是浮点。要查看 WeightCurrent 是否达到了目标重量,建议查看这些值之间的距离是否足够小(见下面的代码示例)。
动画权重系统用于确定如何AnimationTracks在同一优先级下的游戏如何融合在一起。默认重量为一,在重量为零的 AnimationTrack 上无任何运动可见。在任何时间显示的姿势由所有 Poses 和每个 AnimationTrack 的重量平均值决定。在大多数情况下,混合动画不需要,使用 AnimationTrack.Priority 更适合。
代码示例
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.
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 当前重量。它的默认值为 1,当 AnimationTrack:Play() , AnimationTrack:Stop() 或 AnimationTrack:AdjustWeight() 被调用时设置。当重量在 AnimationTrack 设置时,它不会立即更改,而是从重量当前移动到 AnimationTrack.WeightTarget 。执行此操作所需的时间由在动画播放或重量调整时提供的 fadeTime 参数决定。
重量当前可以与 AnimationTrack.WeightTarget 对比,以检查是否达到了所需重量。请注意,这些值不应与 == 运营符相等进行检查,因为这两个值都是浮点。要查看 WeightCurrent 是否达到了目标重量,建议查看这些值之间的距离是否足够小(见下面的代码示例)。
动画权重系统用于确定如何AnimationTracks在同一优先级下的游戏如何融合在一起。默认重量为一,在重量为零的 AnimationTrack 上无任何运动可见。在任何时间显示的姿势由所有 Poses 和每个 AnimationTrack 的重量平均值决定。在大多数情况下,混合动画不需要,使用 AnimationTrack.Priority 更适合。
代码示例
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.
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
这个函数改变了动画的 AnimationTrack.Speed。速度的正值播放动画,负值反向播放,0则暂停它。
动画跟的初始速度设置为 AnimationTrack:Play() 中的参数。但是,播放时可以使轨道的速度发生变化,使用 AdjustSpeed。当速度等于 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).
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.
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
更改动画的重量,其中可选的 fadeTime 参数决定 AnimationTrack.WeightCurrent 到达 AnimationTrack.WeightTarget 的时间长度。
当重量在 AnimationTrack 设置时,它不会立即更改,但会从重量当前移动到 AnimationTrack.WeightTarget 。执行此操作所需的时间由在动画播放或重量调整时提供的 fadeTime 参数决定。
重量当前可以与 AnimationTrack.WeightTarget 对比,以检查是否达到了所需重量。请注意,这些值不应与 == 运营符相等进行检查,因为这两个值都是浮点。要查看 WeightCurrent 是否达到了目标重量,建议查看这些值之间的距离是否足够小(见下面的代码示例)。
动画权重系统用于确定如何AnimationTracks在同一优先级下的游戏如何融合在一起。默认重量为一,在重量为零的 AnimationTrack 上无任何运动可见。在任何时间显示的姿势由所有 Poses 和每个 AnimationTrack 的重量平均值决定。请参阅下面的示例,了解在实践中融合动画的示例。在大多数情况下,混合动画不需要,使用 AnimationTrack.Priority 更适合。
参数
返回
代码示例
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).
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
这个函数返回一个类似于 event 事件的 AnimationTrack.KeyframeReached,除了它只在 KeyframeMarker 中发射,当指定的 animation 被击中时。差异允许更大控制事件何时发触发。
要了解有关使用此函数的更多信息,请参阅 动画事件 在动画编辑器文章中。
还见:
- , 保留在特定时间点的动画中应用到节点的
参数
用于创建的信号名称。不要与 的名称混淆。
返回
当动画到达创建的 KeyframeMarker 时创建和发射的信号。不要与 Keyframe 的名称混淆。
代码示例
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.
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
返回给定名称的第一个 Keyframe 的时间位置在 AnimationTrack 。如果多个 Keyframes 共享相同名称,将返回动画中最早的一个。
此函数如果使用了无效的键帧名称(例如不存在)或基础 Animation 尚未加载,将返回错误。要解决这一问题,请确保只使用正确的键帧名称,并在调用此函数之前加载动画。
要检查动画是否已加载,验证 AnimationTrack.Length 是否大于零。
参数
返回
代码示例
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.
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
当 AnimationTrack:Play() 被调用时,轨道的动画将开始播放,动画的重量将从 0 增加到指定重量(默认为 1),过渡时间为指定的时间(默认为 0.1)。
AnimationTrack 将播放的速度由速度参数决定(默认为 1)。当速度等于 1 时,完成轨道所需的秒数等于轨道的 AnimationTrack.Length 属性。例如,2的速度会导致轨道播放速度加倍。
动画的重量和速度也可以在动画开始播放后使用 AnimationTrack:AdjustWeight() 和 AnimationTrack:AdjustSpeed() 方法进行更改。
如果开发者想使用 AnimationTrack.TimePosition 在特定点开始动画,那么在此之前播放动画是很重要的。
参数
动画的重量应在多长时间内褪色。
动画要播放的重量。
动画的播放速度。
返回
代码示例
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).
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.
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
停止 AnimationTrack 。一旦调用,动画的重量将在指定时间长度内移向零,由可选的 fadeTime 参数确定。例如,如果 Stop() 被调用与 fadeTime 的 2 一起,那么达到零的轨道重量和效果将需要两秒钟。请注意,无论动画的初始重量如何,这都将是如此。
不建议在尝试覆盖此效果并立即终止动画时使用 的 ,因为这会导致关节在原场景冻结。如果必须立即结束,请确保你的装备中的 Motor.MaxVelocity 的 Motor6Ds 高度足够,以便它们能够正确地卡住。
参数
动画重量在几秒内要淡化的时间。
返回
代码示例
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).
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.
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 完全完成移动世界上的任何东西时发生火灾动画已经播放完毕,“渐变”已经完成,主题处于中立姿态。
您可以使用此来采取行动,当动画轨道的主题返回到不受 或 影响的中立姿势时,或清理 。或任何相关连接。
代码示例
The function in this code sample plays an animationTrack and yields until it has stopped and ended, printing at each step along the way.
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 名设置)。这允许通过在动画中添加 Sounds 或 ParticleEffects 在不同时间添加动画的默认功能扩展,从而扩展 Roblox 动画的默认功能。
Keyframe 名称不需要是唯一的。例如,如果动画有三个名为“粒子”的关键帧,那么每当这些关键帧之一被达到时,KeyframeReached事件都会发射。
Keyframe 名称可以在 Roblox 动画编辑器中设置,当创建或编辑动画时。然而,它们不能在播放之前通过 Script 设置在现有动画上。
参数
Stopped
在 AnimationTrack 结束播放时发生火焰。
该事件有多种用途。它可以用来等待直到 AnimationTrack 停止,然后继续 (例如,如果连接一系列动画以在每个之后播放)。它还可以用来清理任何在动画播放期间创建的 Instances 。
代码示例
The function in this code sample will play an animationTrack and yield until it has stopped, before printing.
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)