Better suggestions for animating a 'walking' character sprite
by Colin Richardson · in Torque Game Builder · 07/16/2009 (12:51 pm) · 4 replies
Hello
I'm back again with another of my questions.
I have just got back from our artists a nice sprite sheet with 12 lovely walking frames for our main character.
I've loaded this sprite into TGB, split it up into the Cells and now have Frame 0 to 11 to play with.
So i've created a behaviour to control this sprite (based on the Asteroids Tutorial) so he can move forwards, backwards and rotate left and right fine.
I want to animate him.
So what i've done is to create a method that i call via a schedule when the up/down keys are pressed, and then the method keeps calling itself to keep the animation going, here's that method:
Now my question here is, is this a nice/good way of doing this, or is there a better way of doing it.
We don't plan to have a massive number of 'walking' sprites in the game but i don't want something this simple to gobble resources. I found for example that if i replaced %this.currentFrame with a single call of %this.owner.getFrame(); i would get stutters when there was nothing in the level except for this sprite.
So please what i'm asking is to help a guy who's working on TGB whilst possibly having just starting to suffer from the dreaded Swine Flu with some great suggestions.
Cheers
Colin
I'm back again with another of my questions.
I have just got back from our artists a nice sprite sheet with 12 lovely walking frames for our main character.
I've loaded this sprite into TGB, split it up into the Cells and now have Frame 0 to 11 to play with.
So i've created a behaviour to control this sprite (based on the Asteroids Tutorial) so he can move forwards, backwards and rotate left and right fine.
I want to animate him.
So what i've done is to create a method that i call via a schedule when the up/down keys are pressed, and then the method keeps calling itself to keep the animation going, here's that method:
function CharacterControlsBehavior::changeFrame(%this)
{
if (%this.down > 0 || %this.up >> 0)
{
if (%this.currentFrame < 11)
{
%this.owner.setFrame(%this.currentFrame+1);
%this.currentFrame++;
} else {
%this.owner.setFrame(0);
%this.currentFrame = 0;
}
%this.schedule(100,"changeFrame",%this);
} else {
%this.owner.setFrame(0);
%this.currentFrame = 0;
}
}Now my question here is, is this a nice/good way of doing this, or is there a better way of doing it.
We don't plan to have a massive number of 'walking' sprites in the game but i don't want something this simple to gobble resources. I found for example that if i replaced %this.currentFrame with a single call of %this.owner.getFrame(); i would get stutters when there was nothing in the level except for this sprite.
So please what i'm asking is to help a guy who's working on TGB whilst possibly having just starting to suffer from the dreaded Swine Flu with some great suggestions.
Cheers
Colin
#2
Open up the Animation Builder on the Project menu and build your animation. Under your movement function on the code and add this line:
All in all, this is what my movement function looks like:
My code is based off the shooterControl.cs behavior that is avaible for download on TDN. I still have some bugs on the code(the animation doesn't stop when the player is stopped, I can't mirror the side-moving animation), but it's already a great step for someone that started doing stuff on TGB roughly a month ago.
07/17/2009 (1:01 pm)
That was a problem that I solved myself, but I don't know how to stop the animation when it stops the movement.Open up the Animation Builder on the Project menu and build your animation. Under your movement function on the code and add this line:
%this.owner.playAnimation(NameOfTheAnimation);
All in all, this is what my movement function looks like:
function MyBehavior::moveDown(%this, %val)
{
%this.down = %val;
%this.owner.playAnimation(downAnim);
%this.updateMovement();
}My code is based off the shooterControl.cs behavior that is avaible for download on TDN. I still have some bugs on the code(the animation doesn't stop when the player is stopped, I can't mirror the side-moving animation), but it's already a great step for someone that started doing stuff on TGB roughly a month ago.
#3
07/17/2009 (1:06 pm)
you can have a single frame animation that is the idle animation then when the character stops moving set the animation to the idle one even though it is a single frame
#4
Many thanks for these replies.
The sprite i have has 12 frames and what i do them is to go though the frames (as shown) and then go back to the idle frame when he stops.
That works well though which is nice, as even the most 'away from idle' frame (6/7) aren't to far from idle and so it doesn't look too bad i think.
I'll try the change to the animation, and if it's as easy as that code looks Vinicius Gouveia that all will be good.
Cheers :)
EDIT: I have created a single frame animation, and then another with the full 12 frames.
I then dropped onto the scene the single frame animation, attached the behaviour, modified the behaviour to change the animations when moving/stopping and....
Works very well indeed. Many thanks, i always prefer simpler code.
Cheers!
07/17/2009 (2:50 pm)
Hello,Many thanks for these replies.
The sprite i have has 12 frames and what i do them is to go though the frames (as shown) and then go back to the idle frame when he stops.
That works well though which is nice, as even the most 'away from idle' frame (6/7) aren't to far from idle and so it doesn't look too bad i think.
I'll try the change to the animation, and if it's as easy as that code looks Vinicius Gouveia that all will be good.
Cheers :)
EDIT: I have created a single frame animation, and then another with the full 12 frames.
I then dropped onto the scene the single frame animation, attached the behaviour, modified the behaviour to change the animations when moving/stopping and....
Works very well indeed. Many thanks, i always prefer simpler code.
Cheers!
Torque 3D Owner Aaron Miller
One problem I've had, however, is that the animation may not be on an appropriate frame when the player stops moving and so it looks jerky if you're mid stride and stop. I'm debating about whether or not to try and come up with a bunch of transitions based on the frame or whether or not I want to mark potential stop frames within the animation and force the character to keep moving until I can safely transition.
Hope that helps a bit.