Game Development Community

Problems with Animating sprites

by Mel · in Torque Game Builder · 11/15/2008 (2:19 pm) · 10 replies

I have been working with Torque for a while now and when i say a while i mean...3 months.
Provablly not a "while" but meh...

Anyways here is my problem i am trying to animate my "jumping up", and "falling down" sprites.

The way i have these sprites set up is that my artist made a sprite sheet and i used the "create new image map" to bring in the sprites then i animate them in the Torque interface.

Now my standing animation and running animation is working properly, however when i try to use that code for my "jumping" and "falling sprites"... The program only shows the last sprite of the animation.

I worked with the MiniPlatformer Tutorial and based my code alot off of that tutorial.

i have Torque 1.7.2, just in case there is a unknown problem i do not know about.

here is my SetCurrentAnimation function.

***********************************************************************************

function playerClass::setCurrentAnimation(%this)
{
if(%this.crouch)
{
if(%this.getAnimationName() $= "playerCrouchAnimation")
{
if(%this.getIsAnimationFinished())
{
%this.playAnimation(playerCrouchAnimation);
return;
}
}
else
{
%this.playAnimation(playerCrouchAnimation);
}
}

if (%this.airborne)
{
if(%yVelocity < 0)
{
if(%this.getAnimationName() $= "playerJumpUpAnimation")
{
if(%this.getIsAnimationFinished())
{
%this.playAnimation(playerJumpUpAnimation);
return;
}
}
else
{
%this.playAnimation(playerJumpUpAnimation);
}
}
else
{
if(%this.getAnimationName() $= "playerJumpDownAnimation")
{
if(%this.getIsAnimationFinished())
{
%this.playAnimation(playerJumpDownAnimation);
return;
}
}
else
{
%this.playAnimation(playerJumpDownAnimation);
}
}
}
}
***********************************************************************************

so yeah...the running animation works, as does the standing animation.
However when it is time to call the "jumping up" and "jumping down" animations the Game just calls the the last sprite of the standing animation. And when i want to call the crouch animation, the game again calls the last sprite of the standing animation.

so i replaced the "if(%this.XXXX) statement with a simple code of
%this.playAnimation(XXXX);

and all it does is play the last sprite of each of the animations...
except the "%this.crouch statement...it still plays the last sprite of the standing animation...

So i would appreciate any help any of you could give me.
I will be up all night working on this and some other stuff...then when i get frustrated i watch some "Scrubs"
it's a good formula that equals alot of work output on my half.

I will be up the rest of the night please post your suggestions i appreciate it again, thanks

"Did he also tell you to take your trunks off Turk!? Because the last thing a guy wants to see when he is in a splash pool is his best friends junk heading toward him at 40 miles per hour...
felt like i got Pistol wipped"

~JD, "Scrubs"

#1
11/15/2008 (6:05 pm)
In the tutorial the jumping and falling animations only consist of a single frame. It sounds like you need to go check your animations and make sure they're correctly named and that they have the correct frames.
#2
11/15/2008 (6:19 pm)
Interesting. But the running animation for the tutorial has multiple frames and i figured that if i just copied that code over it would work. Unfortunately it is not working though.
#3
11/15/2008 (8:06 pm)
Did you say they play the last frame of the STANDING animation, and not even the last frame of the correct animation?

Check where you're setting the standing animation and make sure it's not overriding all the IF tests above. Also, just check the names of the animations you're trying to change to - I'm guessing if it can't find an animation with that name, it will just leave it as the last frame of whatever animation it previously had.

I notice you return from the function only if the correct animation is currently being played and has just ended (ie - you're looping the same animation). There's no return when you change the animation (eg - jumping but still playing running animation - you change to the jumping animation but there's no return straight after). Maybe that's where the problem is - might be changing to the correct animation, but because it's not leaving the function it could be processing the standing animation further down. Only guessing, as your whole function isn't shown here.

When you post code, try using [*code] ... [*/code] tags (without the *) - that way, the indenting will be kept and it'll be easier to follow :)
#4
11/16/2008 (4:25 am)
function playerClass::setCurrentAnimation(%this)
{
   %xVelocity = %this.getLinearVelocityX();
   %yVelocity = %this.getLinearVelocityY();

   if (%xVelocity > 0)
   {
      %this.setFlip(true, false);
   }
   else if (%xVelocity < 0)
   {
      %this.setFlip(false, false);
   }
   
   if($player.dashAnimation == true)
   {
      %this.playAnimation(playerDashAnimation);
      return;
   }
      
   if(%this.crouch)
   {
      %this.playAnimation(playerCrouchAnimation);
      return;
   }
   
   if (%this.airborne)
   {
      if(%yVelocity < 0)
      {
         %this.playAnimation(playerJumpUpAnimation);
         return;
      }
      else
      {
         %this.playAnimation(playerJumpDownAnimation);
         return;
      }
   }

   if($player.moveLeft == true || $player.moveRight == true)
   {
      if(%this.getAnimationName() $= "playerRunAnimation")
      {
         if(%this.getIsAnimationFinished())
         {
            %this.playAnimation(playerRunAnimation);
            return;
         }
      }
      else
      {
         %this.playAnimation(playerRunAnimation);
      }
   }
   else
   {
      if(%this.getAnimationName() $= "playerStandAnimation")
      {
         if(%this.getIsAnimationFinished())
         {
            %this.playAnimation(playerStandAnimation);
            return;
         }
      }else
      {
         %this.playAnimation(playerStandAnimation);
      }
   }
}

This is all of my code for Setcurrent animation.
This function succesfully calls standing and running animations properly.
However i did some more debugging and i realized that the game
plays the first sprite of the "jumping up", "jumping down", "crouching", and "dash" animation.

So disregard the "plays the first sprite of the standing animation"

so the animation is called correctly (i checked all of the names of the animations)
however it just stops after the first sprite.
#5
11/16/2008 (7:54 pm)
In the TGB editor, can you see that those animations are > 1 frame? If you mouse-over them, do they play the correct animation?

Is there any reason you have returns after each "stage" here:
if($player.dashAnimation == true)
   {
      %this.playAnimation(playerDashAnimation);
      return; // <<<<<<<<<<<<<<<<<<
   }
      
   if(%this.crouch)
   {
      %this.playAnimation(playerCrouchAnimation);
      return; // <<<<<<<<<<<<<<<<<<<
   }
   
   if (%this.airborne)
   {
      if(%yVelocity < 0)
      {
         %this.playAnimation(playerJumpUpAnimation);
         return; // <<<<<<<<<<<<<<<<<<<
      }
      else
      {
         %this.playAnimation(playerJumpDownAnimation);
         return; // <<<<<<<<<<<<<<<<<<<
      }
   }


but not here?:
if($player.moveLeft == true || $player.moveRight == true)
   {
      if(%this.getAnimationName() $= "playerRunAnimation")
      {
         if(%this.getIsAnimationFinished())
         {
            %this.playAnimation(playerRunAnimation);
            return;
         }
      }
      else
      {
         %this.playAnimation(playerRunAnimation);
// <<<<<<<<<<<<<<<<<<<
      }
   }
   else
   {
      if(%this.getAnimationName() $= "playerStandAnimation")
      {
         if(%this.getIsAnimationFinished())
         {
            %this.playAnimation(playerStandAnimation);
            return;
         }
      }else
      {
         %this.playAnimation(playerStandAnimation);
// <<<<<<<<<<<<<<<<<<<
      }
   }

It would be interesting to see if inserting a return in those spots makes any difference. I suspect maybe not in this case.

Also - at what point are $player.dashAnimation, %this.crouch and %this.airborne set to false? Maybe you're just turning those flags off prematurely? If player lifts a finger from a button, maybe you should check that the current animation is finished playing before you switch back to the standing animation.
#6
11/18/2008 (8:03 am)
Thanks For your help Shaz what i ended up doing is creating a string of else if statements from the running "if" statement. Now my problem is actually stopping the animation so it only goes one cycle and ends on the last sprite...lol
#7
11/18/2008 (3:23 pm)
Lol - isn't that set up in the TGB editor itself - I'm pretty sure there's a checkbox there to say whether the animation should loop or not. Just set it to No for all of them :)
#8
11/19/2008 (12:28 am)
Lol i wish it where that easy...
It was the first thing i tried.

The thing is though is that when i hold the button for "dash animation" i want it to play once and then stop. However it just continues to loop everything...sigh
#9
11/19/2008 (1:37 pm)
Do you set dash to false at some point - on keyup for example? And does your rearrangement of all the IF tests look for the released key?
#10
11/20/2008 (11:59 am)
I set the dash to false on keyUp. And my rearrabged if/else statements look for the released key. Thats no problem. I seperated my animation code in several ways. For instance the string of if/else statements are for animations that i want looped, or the animations that are so short it can't be looped. For instance i have a if(airborne) that plays the jumping up, and down animations, then else if charge jump, then else if wall cling animaition, then i have else if to standing, running, hovering. These animations all look fine, except animnations that do not have a set time i want animated, for example dash, because the user can dash as long as he wants to, and my crouch animation the user can crouch for as long as he wants to. What i might end up doing though is setting up the timer for dash. So that you can not dash continusly, if i set up a controller for dashing i will be able to conrol the animations better. As for crouching...meh it does not need a animation.