Setting a start frame for an animated sprite
by Chad Maron · in Torque Game Builder · 05/20/2005 (10:48 pm) · 10 replies
I've been digging through the source looking for a way to create a setAnimationFrame console function that would set a given animated sprite's frame to x and then continue to play the animation. It looks like it MIGHT be possible by overriding the mCurrentTime property of the fxAnimationController2D but I think that would be a terrible hack that wouldn't really get me any closer to doing what I want. :) Anyone have any ideas?
About the author
#2
So basically yes, I want a single sprite with many animations. There could be some method I've overlooked and if so feel free to tell me to read the manual some more. :)
05/21/2005 (7:23 am)
The basic idea is having a character go from a walking animation to a running animation. Currently if I switch the animation being played it starts from the beginning and that causes the transition from walking to running to jerk a bit. An example of what I am trying to accomplish would be holding down the 'b' button while playing super mario brothers. So basically yes, I want a single sprite with many animations. There could be some method I've overlooked and if so feel free to tell me to read the manual some more. :)
#3
For different animations, just create different animation datablocks, that's what they're for. Have one for running, one for jumping etc.
Hope this helps,
- Melv.
05/23/2005 (12:28 am)
Chad,For different animations, just create different animation datablocks, that's what they're for. Have one for running, one for jumping etc.
Hope this helps,
- Melv.
#4
I have created different datablocks but the issue I have is that all animations start at the first frame (or a random one) by default. In the example of transitioning from walking to running the animations are identical except for the animationTime attribute in the datablock. What happens is if the walking animation is in the middle of a step and you switch the running animation starts from the beginning and you get a small jerk in the motion.
I think I've modified the source to get this to work but I might be better of modifying playAnimation to accept a starting frame. Here is what I've done. In fxAnimationController2D I added a function:
And then in fxAnimatedSprite2D.cc I added the console function:
Sorry if I wasn't clear in my previous posts. Thanks for the response!
-Chad
05/23/2005 (5:11 am)
Melv, I have created different datablocks but the issue I have is that all animations start at the first frame (or a random one) by default. In the example of transitioning from walking to running the animations are identical except for the animationTime attribute in the datablock. What happens is if the walking animation is in the middle of a step and you switch the running animation starts from the beginning and you get a small jerk in the motion.
I think I've modified the source to get this to work but I might be better of modifying playAnimation to accept a starting frame. Here is what I've done. In fxAnimationController2D I added a function:
void setCurrentFrame(U32 newFrame) {
// Calculate the difference between the frame we want and the frame we have
mCurrentTime += (F32)((newFrame-mCurrentFrameIndex)*mFrameIntegrationTime);
};And then in fxAnimatedSprite2D.cc I added the console function:
//-----------------------------------------------------------------------------
// Set Animation Frame.
//-----------------------------------------------------------------------------
ConsoleMethod(fxAnimatedSprite2D, setAnimationFrame, void, 3, 3, "(frameIndex$) - Sets current animation frame.")
{
// Set Animation Frame.
object->mAnimationController.setCurrentFrame(dAtof(argv[2]));
}Sorry if I wasn't clear in my previous posts. Thanks for the response!
-Chad
#5
05/23/2005 (8:58 am)
In other words, it would be nice to be able to specify what frame an animation should start with. I agree with that. :)
#6
05/23/2005 (9:04 am)
Heh.. yes. I made the changes to playAnimation but have not had time to fully test... there are a few places where playAnimation is called in the source. I'm toying with the idea of wrapping the playAnimation function in another function to avoid changing a lot of the code, but, that's a hack and a half.
#7
In fxAnimationController2D.h:
in fxAnimationController2D.cc:
In fxAnimatedSprite2D.h:
And finally the new console bits in fxAnimatedSprite2D.cc:
I think that's all the changes I have made. Thanks to the glory of method overloading I didn't have to change any other source for this to work.
05/25/2005 (6:18 am)
Ok I finally have a few minutes to post my changes.... In fxAnimationController2D.h:
bool playAnimation( const char* animationName, bool autoRestore , U32 startFrame); bool playAnimation( const char* animationName, bool autoRestore);
in fxAnimationController2D.cc:
bool fxAnimationController2D::playAnimation( const char* animationName, bool autoRestore ) {
return playAnimation( animationName, autoRestore, 0);
}
bool fxAnimationController2D::playAnimation( const char* animationName, bool autoRestore, U32 startFrame )
{
//... snip
// Reset Current Time.
if ( pAnimationDataBlock->mRandomStart )
mCurrentTime = mGetT2DRandomF(0.0f, mTotalIntegrationTime*0.999f);
else {
mCurrentTime = (F32)(startFrame*mFrameIntegrationTime);
}
//... rest of method
}In fxAnimatedSprite2D.h:
bool playAnimation( const char* animationName, bool autoRestore, U32 startFrame);
And finally the new console bits in fxAnimatedSprite2D.cc:
//-----------------------------------------------------------------------------
// Play Animation.
//-----------------------------------------------------------------------------
ConsoleMethod(fxAnimatedSprite2D, playAnimation, bool, 3, 5, "(animationName$, [autoRestore?], [startFrame$]) - Plays an animation.")
{
// Play Animation (option auto-restore animation flag).
return object->playAnimation( argv[2], argc==4 ? dAtob(argv[3]) : false, argc==5 ? dAtof(argv[4]) : 0 );
}
bool fxAnimatedSprite2D::playAnimation( const char* animationName, bool autoRestore, U32 startFrame )
{
// Reset Animation Callback.
mAnimationCallbackComplete = false;
// Play Animation.
return mAnimationController.playAnimation( animationName, autoRestore, startFrame );
}I think that's all the changes I have made. Thanks to the glory of method overloading I didn't have to change any other source for this to work.
#8
I'll get something similar to this add to the SDK permanently.
- Melv.
05/25/2005 (12:15 pm)
Chad,I'll get something similar to this add to the SDK permanently.
- Melv.
#9
11/16/2005 (3:55 pm)
Hey melv, when will the new sdk be available? (the wich include animated sprite funtions?)
Torque Owner Ted "darkhitman" Nubel