# How should I preform an animation only once?

This topic is 4153 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

On a button press, I'd like a character in my game to preform an animation, then switch back to the looping idle animation. I remember reading about an API call to do a nonlooping anitmation, but browsing through the MSDN doesn't bring up anything. The only thing I can think of is keeping track of how long the animation has been playing and stopping it once it goes over it's duration, but that seems rather hackish, especially if there is indeed an API call that will do it for me. Am I just imagining what I read? Or is there an even better way than my workaround? edit: I'm not sure if it was apparent, but this is for a 3d model.

##### Share on other sites
Hey - I had to do the same kind of thing. I figured it is a very common thing that people need, but I didn't see anything like what you describe, either. I ended up getting the period of the animation set (ID3DXAnimationSet::GetPeriod()) then using KeyTrackSpeed() to set the track's speed to 0.0 at GetTime()+period-0.05.

The -0.05 is to make sure that the animation doesnt wrap around at the end before the event kicks in. I used KeyTrackSpeed() instead of KeyTrackEnable() becuase it behaved a lot better. I also used GetUpcomingTrackEvent() to see if the mesh is still animating or not. No upcoming event means it has stopped.

Maybe someone has a more professional way of doing this.

##### Share on other sites
Here's the code that I've implemented. It's similar to yours, but I think I worked around needing the +.05 part.
// Is the animation a one time loop? If so, animate the mesh until the period is// over. If the elapsed time is greater than the period, find the difference and// play that far into the idle animationif( !m_Looping ) {	if( m_LoopTime > deltaTime )		m_LoopTime -= deltaTime;	else {        	deltaTime -= m_LoopTime;		this->SetAnimation( "Idle" );	}}

I'm noticing that if I switch animations really fast, the character will sometimes animate a tad farther into the animation than I want. This won't be possible in the finished game, but I'd like to fix it anyways. I don't think it's a problem with the above code, though.

If anyone has a better solution, I'd love to hear it, as this still feels rather hackish.

edit: I was able to fix the "animate further into an animation than needed" problem by switching to the idle animation for zero seconds before changing to the new animation. I'm not sure why that helped, though. Is there a HaltAnimation() call anywhere? I'm sure that would do the job, too.

[Edited by - MasterSlowPoke on March 8, 2007 3:01:49 PM]

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002218
×