- Изменено
check if animation is playing?
I've just started implementing some animations into my game. I'm trying to choose animations depending on what the player is doing. Problem is, if, for example, "jump" is already playing, setting jump animation to true just resets the animation. How can I check if a specific animation is playing so that I can avoid calling it again?
if (!grounded)
{
skeleAnim.state.SetAnimation(0, "jump", true);
var trackEntry = skeleAnim.state.GetCurrent(0);
trackEntry.TimeScale = 1;
}else
{
if(myRigidbody2D.velocity.x == 0)
{
skeleAnim.state.SetAnimation(0, "idle", true);
var trackEntry = skeleAnim.state.GetCurrent(0);
trackEntry.TimeScale = 0.5f;
}
else
{
skeleAnim.state.SetAnimation(0, "run", true);
var trackEntry = skeleAnim.state.GetCurrent(0);
trackEntry.TimeScale = 3.5f;
}
}
18 Nov 2016 7:25 am
So I've solved my issue. Now I'm using this line to check if the animation is running:
if (skeleAnim.state.GetCurrent(0).ToString() != "jump")
I've got two more follow up questions:
Is there a better way of checking GetCurrent(0) without using .ToString()?
How can I switch animations in a way that makes the blend speed faster?
You can set the mix times in the AnimationStateData or TrackEntry mixDuration
. See here:
Applying Animations - Spine Runtimes Guide: Mix times
To be more efficient checking your animation:
Animation jumpAnimation = skeletonData.findAnimation("jump");
...
if (skeleAnim.state.GetCurrent(0) != jumpAnimation) { ... }
If using ToString, keep in mind GetCurrent can return null if no animation is playing.
Thanks for your response! This is everything I needed to know.
It's actually
Animation jumpAnimation = skeletonAnimation.skeleton.Data.FindAnimation("jump");
bool isPlayingJump = (skeletonAnimation.state.GetCurrent(0).Animation == jumpAnimation);
GetCurrent
returns a TrackEntry object which represents the container of the currently-playing Animation (.Animation
) or null if the track is not playing an animation.
Pharan написалIt's actually
Animation jumpAnimation = skeletonAnimation.skeleton.Data.FindAnimation("jump"); bool isPlayingJump = (skeletonAnimation.state.GetCurrent(0).Animation == jumpAnimation);
GetCurrent
returns a TrackEntry object which represents the container of the currently-playing Animation (.Animation
) or null if the track is not playing an animation.
Ive always thought there should be a better way of checking if an animation is playing rather than string comparing
so skeletonAnimation.skeletonData doesn't seem to exist for me. I only have something called .skeletonDataAsset available, which doesn't contain FindAnimation().
Error CS1061 'SkeletonAnimation' does not contain a definition for 'skeletonData' and no extension method 'skeletonData' accepting a first argument of type 'SkeletonAnimation' could be found (are you missing a using directive or an assembly reference?)
19 Nov 2016 9:29 am
Figured it out. For those interested, this is what worked for me:
Spine.Animation runAnimation; //had to use Spine.Animation because Unity already has an "Animation" type.
runAnimation = skeletonAnimation.skeleton.Data.FindAnimation("run"); //had to use skeleton.Data rather than skeletonData
Sorry, it had a missing dot. it's skeleton.Data.
That's correct.
For Spine runtime within Phaser, is there some sort of ignoreIfPlaying functionality?
SpineGameObject (the old way) seemed to have an ignoreIfPlaying (see old docs)
The new runtime doesn't seem to have such functionality.
I'm wondering if this is not common enough / there is a better way to handle the game code such that this functionality is not really needed? (the typical case for me is that on every update I evaluate some logic which tries to set an animation, but this is problematic if the previous update also evaluated same logic which tried setting the same animation, hence the animation visually never runs since it keeps getting started, similar issue as in the original post)
And in case the pattern is indeed not uncommon (setting animation on every update and having to prevent setting it if already running), is it still the recommended way what @Nate suggested above Nate long time ago?
Thank you so much for your time and help!
Yes, all our runtimes have similar APIs and checking the animation that is playing on a track is the best way to know what is playing.
For an example in real app code, this only sets a new animation if the current animation is different:
EsotericSoftware/spine-superspineboyblob/master/src/com/esotericsoftware/spine/superspineboy/CharacterView.java#L56