AnimationTrack

Pokaż przestarzałe

*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.

Brak możliwości tworzenia

Kontroluje odtwarzanie animacji na AnimationController . Ten obiekt nie może być utworzony, zamiast tego jest zwracany przez metodę Animator:LoadAnimation().

Podsumowanie

Właściwości

  • Tylko do odczytu
    Bez replikacji
    Odczyt równoległy

    Przedmiot Animation, który używany był do stworzenia tego AnimationTrack.

  • Tylko do odczytu
    Bez replikacji
    Odczyt równoległy

    Własność czytelna, która zwraca prawdę, gdy AnimationTrack gra.

  • Tylko do odczytu
    Bez replikacji
    Odczyt równoległy

    Czytelna właściwość, która zwraca długość (w sekundach) AnimationTrack . To będzie zwracać 0, aż animacja będzie w pełni załadowana i tym samym może nie być natychmiastowo dostępna.

  • Odczyt równoległy

    Ustawia, czy animacja zostanie powtórzona po zakończeniu. Jeśli jest zmieniana podczas gry, efekt będzie miał miejsce po zakończeniu animacji.

  • Odczyt równoległy

    Ustawia priorytet AnimationTrack . W zależności od tego, co jest ustawione, grając wiele animacji na raz będzie wyglądać na tę właściwość, aby określić, która Class.Keyframe``Class.Pose|Poses powinna być grana nad drugą.

  • Tylko do odczytu
    Bez replikacji
    Odczyt równoległy

    Prędkość AnimationTrack jest wartością czytelną, która daje bieżącą szybkość odtwarzania AnimationTrack. Ma domyślną wartość 1. Gdy prędkość jest równa 1, czas potrzebny na zakończenie animacji jest równy AnimationTrack.Length (w sekundach).

  • Bez replikacji
    Odczyt równoległy

    Zwraca pozycję w czasie w sekundach, że AnimationTrack jest poprzez gry jego animacjaźródła. Można ustawić, aby skoczny tor do określonej chwili w animacja.

  • Tylko do odczytu
    Bez replikacji
    Odczyt równoległy

    Własność tylko do czytania, która daje obecną wagę AnimationTrack . Ma ona domyślną wartość 1.

  • Tylko do odczytu
    Bez replikacji
    Odczyt równoległy

    Własność tylko do czytania, która daje obecną wagę AnimationTrack .

Metody

Zdarzenia

Właściwości

Animation

Tylko do odczytu
Bez replikacji
Odczyt równoległy

Obiekt Animation, który został użyty do stworzenia tego AnimationTrack. Aby stworzyć AnimationTrack, musisz załadować obiekt 2>Class.Animation2> na 5>Class.AnimationController5> przy użyciu metody 8>Class.Animator:LoadAnimation()8>.

Przykłady kodu

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

Tylko do odczytu
Bez replikacji
Odczyt równoległy

Własność czytelna, która zwraca prawdę, gdy AnimationTrack gra.

Ta właściwość może być używana przez rozwój, aby sprawdzić, czy animacja jest już grająca przed jej uruchomieniem (ponieważ to spowodowałoby ponowne uruchomienie). Jeśli rozwój chce uzyskać wszystkie grające AnimationTracks na Humanoid lub Class.AnimationController</

Przykłady kodu

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

Tylko do odczytu
Bez replikacji
Odczyt równoległy

Czytelna właściwość, która zwraca długość (w sekundach) AnimationTrack . To będzie zwracać 0, aż animacja będzie w pełni załadowana i tym samym może nie być natychmiastowo dostępna.

Gdy AnimationTrack.Speed Class.AnimationTrack jest równy 1, animacja będzie wymagała AnimationTrack.Length (w sekundach) do ukończenia.

Przykłady kodu

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

Odczyt równoległy

Ta właściwość ustawia, czy animacja zostanie powtórzona po zakończeniu. Jeśli jest zmieniana podczas gry, efekt będzie miał miejsce po zakończeniu animacji.

Właściwość Looped dla AnimationTrack domyślnie ustawiona jest na sposób, w jaki została ustawiona w edytorze animacji. Jnak można ją zmienić, umożliwiając kontrolę nad AnimationTrack podczas uruchomienia gry. Looped również poprawnie zarządza animacjami odtwarzanymi w odwrotny sposób

Ta właściwość umożliwia rozwinięcie i niewykonanie wariantu animacjatego samego modelu bez potrzeby wstawiania dwóch wersji do Roblox.

Przykłady kodu

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)
Odczyt równoległy

Ta właściwość ustawia priorytet Class.AnimationTrack . W zależności od tego, co jest ustawione, grając wiele animacji na raz będzie wyglądać na tę właściwość, aby określić, która Class.AnimationTrack``Class.Keyframe powinna być grana nad drugą.

Właściwość priorytetu dla AnimationTrack domyślnie ustawia się i publikuje z Studio's Animation Editor . Używa Enum.AnimationPriority, która ma 7 poziomów priorytetu:

  1. Akcja 4 (najwyższy priorytet)
  2. Akcja3
  3. Akcja2
  4. Akcja
  5. Ruch
  6. Nieaktywny
  7. Rdzeń (najniższy priorytet)

Prawidłowo ustaw priorytety animacji, poprzez edytor lub poprzez tę właściwość, pozwolić na odtwarzanie wielu animacji bez ich kolidowania. Gdy dwa odtwarzające animacje kierują celu do poruszania tego samego kończyna w różnych sposób, w AnimationTrack z najwyższą priorytetem będzie pokazywać, prezentować. Jeśli obie animacje mają ten sam priorytet, w

Ta właściwość umożliwia również programistom odtwarzanie tej samej animacji w różnych priorytetach, bez konieczności wstawiania dodatkowych wersji do Roblox.

Speed

Tylko do odczytu
Bez replikacji
Odczyt równoległy

Prędkość AnimationTrack jest wartością czytelną, która daje bieżącą szybkość odtwarzania AnimationTrack. Ma domyślną wartość 1. Gdy prędkość jest równa 1, czas potrzebny na zakończenie animacji jest równy AnimationTrack.Length (w sekundach).

Jeśli prędkość jest dostosowana, to czas odtwarzania toru można obliczyć, dzieląc długość przez prędkość. Prędkość jest ilością niezdefiniowaną.

Prędkość można używać do łączenia długości animacji z różnymi wydarzeniami gry (na przykład ładowaniem innych wariantów tej samej animacji) bez konieczności wstawiania różnych wariantów tej samej animacji.

Właściwość ta jest czytelna tylko, a możesz ją zmienić używając AnimationTrack:AdjustSpeed() .

Przykłady kodu

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

Bez replikacji
Odczyt równoległy

Zwraca pozycję w czasie w sekundach, że AnimationTrack jest poprzez gry jego animacjaźródła. Można ustawić, aby skoczny tor do określonej chwili w animacja.

Pozycja czasowa może być ustawiona, aby przeprowadzić się do określonego punktu w animacja, ale AnimationTrack musi grać, aby to zrobić. Można również używać w połączeniu z AnimationTrack:AdjustSpeed() , aby zamrozić animację na określonym punkcie (ustawiając prędkość na 0).

Przykłady kodu

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

Tylko do odczytu
Bez replikacji
Odczyt równoległy

Gdy masa jest ustawiona w AnimationTrack nie zmienia się natychmiastowo, ale przesuwa się od WeightCurrent do AnimationTrack.WeightTarget. Czas potrzebny na wykonanie tego jest zalecony przez parametr fadeTime podany, gdy animacja jest odtwarzana, lub dostosowana.

WeightCurrent można sprawdzić przeciwko AnimationTrack.WeightTarget , aby ujrzeć, czy pożądany wynik został osiągnięty. Uwaga, że te wartości nie powinny być sprawdzane w celu równości z operatorem ==, ponieważ obie te wartości są floatami. Aby ujrzeć, czy WeightCurrent osiągnął pożądany wynik, należy sprawdzić, czy dystans między tymi w

System wagi animacji jest używany do określenia, jak AnimationTracks grający na tej samej priorytecie jest mieszany z innymi. Domyślną wagą jest jeden

Przykłady kodu

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

Tylko do odczytu
Bez replikacji
Odczyt równoległy

AnimationTrack.WeightTarget jest czytelnym jedynym wartością, która daje obecną wagę AnimationTrack. Ma domyślną wartość 1

WeightCurrent można sprawdzić przeciwko AnimationTrack.WeightTarget , aby ujrzeć, czy pożądany wynik został osiągnięty. Uwaga, że te wartości nie powinny być sprawdzane w celu równości z operatorem ==, ponieważ obie te wartości są floatami. Aby ujrzeć, czy WeightCurrent osiągnął pożądany wynik, należy sprawdzić, czy dystans między tymi w

System wagi animacji jest używany do określenia, jak AnimationTracks grający na tej samej priorytecie jest mieszany z innymi. Domyślną wagą jest jeden

Przykłady kodu

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

Metody

AdjustSpeed

void

Funkcja ta zmienia AnimationTrack.Speed animacja. Pozytywna wartość dla szybkości odtwarza animację do przodu, a negatywna odtwarza ją do tyłu, a 0 wstrzyma ją.

Początkowa szybkość AnimationTrack ustawiona jest jako parametr w AnimationTrack:Play(). Ale szybkość toru można zmienić podczas odtwarzania, używając AdjustSpeed. Gdy szybkość jest równa 1, czas trwania animacji jest równy AnimationTrack.Length (w sekundach).

Gdy jest dostosowany, można obliczyć czas odtwarzania toru poprzez podzielenie długości przez prędkość. Prędkość jest ilością niezdefiniowaną.

Prędkość można używać do łączenia długości animacji z różnymi wydarzeniami gry (na przykład ładowaniem innych wariantów tej samej animacja).

Parametry

speed: number

Szybkość odtwarzania animacji ma być zmieniona.

Wartość domyślna: 1

Zwroty

void

Przykłady kodu

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

Zmienia wagę animacja, z opcjonalnym parametrem fadeTime określającym czas trwania AnimationTrack.WeightCurrent do osiągnięcia AnimationTrack.WeightTarget .

Gdy masa jest ustawiona w AnimationTrack nie zmienia się natychmiastowo, ale przesuwa się od WeightCurrent do AnimationTrack.WeightTarget. Czas potrzebny na wykonanie tego jest zalecony przez parametr fadeTime podany, gdy animacja jest odtwarzana, lub dostosowana.

WeightCurrent można sprawdzić przeciwko AnimationTrack.WeightTarget , aby ujrzeć, czy pożądany wynik został osiągnięty. Uwaga, że te wartości nie powinny być sprawdzane w celu równości z operatorem ==, ponieważ obie te wartości są floatami. Aby ujrzeć, czy WeightCurrent osiągnął pożądany wynik, należy sprawdzić, czy dystans między tymi w

System ważenia animacji jest używany do określenia, jak AnimationTracks grające na tej samej priorytecie są mieszane razem.

Parametry

weight: number

Waga animacji, którą chcesz zmienić.

Wartość domyślna: 1
fadeTime: number

Czas trwania animacji między starym a nowym ważeniem.

Wartość domyślna: 0.100000001

Zwroty

void

Przykłady kodu

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

Funkcja ta zwraca event podobny do wydarzenia AnimationTrack.KeyframeReached, z wyjątkiem tego, że działa tylko wtedy, gdy określony KeyframeMarker został dotknięty w 2> Class.Animation|animation . Różnica umożliwia większą kontrolę tego, k

Aby dowiedzieć się więcej o używaniu tej funkcji, zobacz Wydarzenia animacji w artykule Redaktor animacji.

Więcej o kluczowych ramach

Keyframe może być ustawiona w Roblox Animation Editor podczas tworzenia lub edytowania animacja. Nie może jednak być ustawione przez Class.Script na istniejącej animacji przed jej uruchomieniem.

Keyframe nie musi być unikalna. Na przykład, jeśli Animation ma trzy kluczowe ramy nazwane "EmitParticles", połączone wydarzenie z tej funkcji będzie się włączać za każdym razem, gdy jeden z tych kluczowych ram zostanie osiągnięty.

Zobacz również:

Parametry

name: string

Nazwa KeyFrameMarker sygnału, który jest tworzony.


Zwroty

Znaki stworzone i wystrzelone, gdy animacja dotrze do stworzonego KeyFrameMarker .

Przykłady kodu

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

Zwraca pozycję czasową pierwszego Keyframe z podanego nazwy w AnimationTrack. Jeśli wiele Keyframes dzieli tę samą nazwę, zwróci najpierw najstarszy w animacja.

Funkcja ta będzie zwracać błąd, jeśli zostanie używana z nieprawidłowym imieniem kluczowego ramy (takiego jak nieistniejące Animation lub jeśli podstawowy Class.Animation nie został jeszcze załadowany. Aby to naprawić, upewnij się, że tylko poprawne imiona kluczowych ram są używane i animacja zostanie załadowana przed uruchomieniem funkcji.

Aby sprawdzić, czy animacja została załadowana, zweryfikuj, że AnimationTrack.Length jest większa niż zero.

Parametry

keyframeName: string

Nazwa związana z Keyframe do znalezienia.


Zwroty

Czas, w sekundach, Keyframe następuje w normalnej szybkości odtwarzania.

Przykłady kodu

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

Gdy AnimationTrack:Play() zostanie wywołany, zacznie się grać animacja toru, a waga animacji będzie zwiększana od 0 do określonej wagi (domyślnie 1) przez określony czas odblasku (domyślnie 0,1).

Prędkość, z jaką gra AnimationTrack, jest zalecona przez parametr prędkości (domyślnie 1). Gdy prędkość jest równa 1, liczba sekund, które będą potrzebne do ukończenia, jest równa właściwości AnimationTrack.Length tracku. Na przykład, prędkość 2 powoduje, że track będzie grać dwa razy szybciej.

Animacja może być również zmieniona po uruchomieniu animacji używając metod AnimationTrack:AdjustWeight() i AnimationTrack:AdjustSpeed().

Jeśli rozwój chce uruchomić animację w określonym punkcie używając AnimationTrack.TimePosition , to ważne, aby animacja była odtwarzana przed tym, co zostanie zrobione.

Parametry

fadeTime: number

Czas trwania, przez którego ciepło animacjapowinno zaniknąć.

Wartość domyślna: 0.100000001
weight: number

Waga animacji, która ma być odtwarzana.

Wartość domyślna: 1
speed: number

Szybkość odtwarzania animacja.

Wartość domyślna: 1

Zwroty

void

Przykłady kodu

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

Zatrzymuje AnimationTrack. Po wezwaniu odtwarzania zatrzyma się i masa animacji przeniesie się w kierunku zero w określonym czasie zgodnie z parametrem czasu wygasania opcjonalnego parametru fadeTime.

Na przykład, jeśli Stop jest wzywany z czasą opóźnienia 2 sekund, to zajmie to dwa sekundy na wagę Class.AnimationTrack . Proszę zauważyć, że będzie to dotyczyć bez względu na początkową wagę animacja.

Nie jest rekomendowane używanie czasu wygasania 0 sekund, aby spróbować tego efektu i natychmiastowo zakończyć animację, co powoduje, że pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy pozy po

Parametry

fadeTime: number

Czas, w sekundach, dla którego mikro animacja wiecznie zniknie.

Wartość domyślna: 0.100000001

Zwroty

void

Przykłady kodu

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)

Zdarzenia

DidLoop

Ten wążdźca zdarza się za każdym razem, gdy AnimationTrack zakończony w pętli, w następnym aktualizacja.

Może on również wystrzelić na dokładnym końcu niespustosnej animacji, ale to zachowanie nie powinno być wiarygodne.


Przykłady kodu

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

Wystrzela, gdy AnimationTrack jest całkowicie skończony z przesuwaniem czegokolwiek w świecie. Animacja zakończyła się, a „fade out” jest zakończony, a temat jest w neutralnej pozy.

Możesz użyć tego, aby podjąć działanie, gdy przedmiot animacji śledu powróci do neutralnej pozy, która nie jest dotknięta przez AnimationTrack lub czyszczenie AnimationTrack. lub dowolne powiązane połączenia.


Przykłady kodu

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

Wyst?puje za ka?dym razem, gdy odtwarzanie Class.AnimationTrack osi?ga AnimationTrack, kt?re nie ma domy?lnej nazwy - "Keyframe".

Ten wątek pozwala rozwinięciu kodu na określonych punktach w animacji (zdefiniowane przez Keyframe nazwy). Dzięki temu możliwe jest rozszerzenie domyślnej funkcjonalności animacji Roblox poprzez dodanie Sounds lub ParticleEffects na różnych punktach w animacja.

Keyframe nie musi być unikalna. Na przykład, jeśli animacja ma trzy kluczowe ramy nazwane „Cząsteczki”, wtedy wydarzenie KeyframeReached będzie się włączać za każdym razem, gdy jeden z tych kluczowych ram zostanie osiągnięty.

Keyframe może być ustawiona w Roblox Animation Editor podczas tworzenia lub edytowania animacja. Nie może jednak być ustawiona przez Script na istniejącej animacji przed jej uruchomieniem.

Parametry

keyframeName: string

Nazwa Keyframe osiągnięta.


Stopped

Wyst?puje za ka?dym razem, gdy AnimationTrack sko?czy gra?.

Ten wątek ma kilka użyć. Można go użyć do czekania, aż AnimationTrack przestanie działać, zanim będzie można kontynuować (na przykład, jeśli łączysz serię animacji do odtwarzania po sobie). Można również użyć go do czyszczenia dowolnych Instances stworzonych podczas odtwarzania animacji.


Przykłady kodu

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)