Stoping an Animated Sprite
by James Daniel · in Torque Game Builder · 03/07/2007 (2:54 pm) · 9 replies
I see how to start an animated sprite when an event happens, what I need is a way to stop an animated sprite. What I have is a sprite with a set of gears (the animated sprite)that are mounted to the it and are to run only when the sprite is moving.
I've been searching through the documentation and forums and can't seem to find anything on stoping an animated sprite once it is started until it reaches the last frame.
Can anyone help?
I've been searching through the documentation and forums and can't seem to find anything on stoping an animated sprite once it is started until it reaches the last frame.
Can anyone help?
#2
03/07/2007 (5:08 pm)
No crazy gears, but the effect looks best when the gears stop moving when the sprite stops moving. Thank of this like tires on a car. I'll give the setAnimationFrame, getAnimationFrame a try and see what happens. I don't have source so adding a stop animation function is not an option.
#3
Try this
Obviously replace %animatedobject with the reference to your animated object.
03/13/2007 (5:03 pm)
Not sure if this will work for you or not but I thought I would throw it out there anyway. You can't stop the animation from script but you can pause it. Try this
//Pause the animation %animatedobject.setpause(true); //Resume the animation %animatedobject.setpause(false);
Obviously replace %animatedobject with the reference to your animated object.
#4
If anyone is interested I'll post what I get working.
03/13/2007 (8:26 pm)
Thanks for the suggestion but it turns out that I'm going to need a lot more control over an animated sprite than what is currently available. I need to control direction of animation, forward & backward, along with dynamically controlling the speed of the animation. Since I don't have the code I'm going to have to make do with static sprites and write a script to control the actual animation. If anyone is interested I'll post what I get working.
#5
I started playing around with the animation functions and variables and found I can do everything you mention in your post.
The key is in the animation datablock and changing those variables dynamically.
An animation datablock looks like this
When you put an animated image in your level, TGB creates a t2dAnimatedSprite in the level file like this
what you can do in script is set the fields in the animationdatablock. This will not effect the currently running animation but you can reload the animation with %animatedsprite.setanimation("animationDataBlock")
There is also a per frame callback that you could utilize to adjust things.
Just thinking out loud. This will allow you to still leverage TBG's animation capabilities and gain finer control over the animation. You could emulate all this with script but why not take advantage of the built in functionality.
Good luck! I would enjoy seeing what you come up with.
03/13/2007 (10:50 pm)
You got me thinking about how much control you have over animation.I started playing around with the animation functions and variables and found I can do everything you mention in your post.
The key is in the animation datablock and changing those variables dynamically.
An animation datablock looks like this
new t2dAnimationDatablock(molelilacAnimation) {
imageMap = "molelilacImageMap";
animationFrames = "0 1 2 3 4 5 6 7 8";
animationTime = "1";
animationCycle = "0";
randomStart = "0";
startFrame = "0";
};This is what TGB creates when you create an animated sprite. You can find the datablock in the datablocks.cs file in the managed directory. When you put an animated image in your level, TGB creates a t2dAnimatedSprite in the level file like this
new t2dAnimatedSprite(mole) {
animationName = "molelilacAnimation";
canSaveDynamicFields = "1";
position = "-1.852 -3.303";
size = "9.125 16.000";
mountID = "2";
};what you can do in script is set the fields in the animationdatablock. This will not effect the currently running animation but you can reload the animation with %animatedsprite.setanimation("animationDataBlock")
//These are the fields in our datablock
molelilacAnimation.animationFrames = "8 7 6 5 4 3 2 1 0"; //reverse the frame order of the animation
molelilacAnimation.animationTime = 5; //change the animation time
%frameCount = getWordCount( molelilacAnimation.animationFrames ); // How many frames
//Now control the animated sprite
mole.setpaused(true); //stop the animation
%curFrame = mole.getcurrentframe() ; //Find out where we stopped in the animation so
// when we reverse we can start from the same frame so it flows
%newFrame = %frameCount - %curFrame; //Calculate our start frame because we reverse the order
molelilacAnimation.startFrame = %newFrame; // set our starting frame
mole.setAnimation("molelilacAnimation"); //This will restart the animation with the new parametersThis is a just simplified example to show the capabilites. There is also a per frame callback that you could utilize to adjust things.
Just thinking out loud. This will allow you to still leverage TBG's animation capabilities and gain finer control over the animation. You could emulate all this with script but why not take advantage of the built in functionality.
Good luck! I would enjoy seeing what you come up with.
#6
You could simply use the autorotation on the static sprite. I think that would give a smoother look and be WAY easier to implement. %gear.setAutoRotation(%degreesPerSecond). you can specify positive or negative values and that would give you the directional control. To stop the rotation just set autorotation to 0.
If your animation is a set of gears you could create them as individual sprites and autorotate the individual sprites.
03/13/2007 (11:09 pm)
Hmmm, now that I think more about it, you said you were trying to animate gears. I am assuming they are circular. You could simply use the autorotation on the static sprite. I think that would give a smoother look and be WAY easier to implement. %gear.setAutoRotation(%degreesPerSecond). you can specify positive or negative values and that would give you the directional control. To stop the rotation just set autorotation to 0.
If your animation is a set of gears you could create them as individual sprites and autorotate the individual sprites.
#7
I give the autorotation a try first and see how that looks. If it doesn't give the effect I need I'll use your suggestion to use the t2dAnimationDatablock to give me the control I need.
03/14/2007 (2:21 pm)
Thanks for the help Guy. I give the autorotation a try first and see how that looks. If it doesn't give the effect I need I'll use your suggestion to use the t2dAnimationDatablock to give me the control I need.
#8
Here is the script code
Everything works but the change to the animationFrames, any idea what's wrong?
03/23/2007 (3:17 pm)
Got an interesting problem with the using the scripts you gave me. It seems that the changing of the animationFrames in the t2dAnimationDatablock has no effect, that is there is no reversal of the animation.Here is the script code
new t2dAnimationDatablock(FunnelAnimation) {
imageMap = "FunnelImageMap";
animationFrames = "0 1 2 3 4 5 6 7 8 9 10 11 12 13";
animationTime = "1";
animationCycle = "0";
randomStart = "0";
startFrame = "0";
};new t2dAnimatedSprite(Funnel) {
animationName = "FunnelAnimation";
canSaveDynamicFields = "1";
class = "FunnelClass";
Position = "-56.585 -70.298";
size = "23.750 10.000";
WorldLimitMode = "CLAMP";
WorldLimitMin = "-71.503 -76.512";
WorldLimitMax = "84.105 54.915";
mountID = "14";
};function FunnelClass::onLevelLoaded(%this, %scenegraph)
{
$Funnel = %this;
//These are the fields in our datablock
FunnelAnimation.animationFrames = "13 12 11 10 9 8 7 6 5 4 3 2 1 0"; //set the frame order of the animation
FunnelAnimation.startFrame = "0"; // set our starting frame
FunnelAnimation.animationCycle = "1";
Funnel.setAnimation("FunnelAnimation");
$Funnel.setLinearVelocityX(15);
}Everything works but the change to the animationFrames, any idea what's wrong?
#9
Hmmm ya, sorry about that. Digging into further and looking what I had done and the differences seem to be that you are setting a looping animation. From digging through the source it looks like it will never attempt to reload the animation even if you set the animation when it is looping.
What you might try is set a new datablock with the reverse animation and set the appropriate animation block instead of trying to change the frame sequence. I tested that out and it seems to work.
If that doesn't work out you will probably have to modify the source to include the extra functionallity.
Good luck!
03/24/2007 (5:37 pm)
@JamesHmmm ya, sorry about that. Digging into further and looking what I had done and the differences seem to be that you are setting a looping animation. From digging through the source it looks like it will never attempt to reload the animation even if you set the animation when it is looping.
What you might try is set a new datablock with the reverse animation and set the appropriate animation block instead of trying to change the frame sequence. I tested that out and it seems to work.
If that doesn't work out you will probably have to modify the source to include the extra functionallity.
Good luck!
Associate Tom Eastman (Eastbeast314)
If that's not good enough, you could try using a series of shorter, script-connected animations or play with setAnimationFrame and getAnimationFrame to see if setting the frame stops the animation.
If you have source, you could easily add a function like setFrameAndStop too.
Hope one of those sounds good ;)