Game Development Community

Problems with animated sprites

by Kuan Hanrong · in Torque Game Builder · 08/10/2009 (7:23 pm) · 3 replies

Hi, just started working with TGB and I'm having a bit of an issue with animated sprites. Basically, I'm having problems playing the idle and jump animations for my character. The animation seems to be stuck on the 1st frame. However, there isn't any issue with the running animation.

Here's my chunk of code of my player.cs, taken from the Platformer tutorial:

function playerClass::onLevelLoaded(%this, %scenegraph)
{
  // Store a reference to the player's ship in a global variable
  $pEri = %this;
  
   moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
   moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
   moveMap.bindCmd(keyboard, "space", "playerJump();", "");
   moveMap.bindCmd(keyboard, "lcontrol", "playerShoot();", "");
   %this.setCollisionMaxIterations(2);

   %force = 20;
   sceneWindow2D.mount($pEri, "0 0", %force, true);
   sceneWindow2D.setViewLimitOn("-150 -37 800 37");


}

function playerLeft()
{
    $pEri.moveLeft = true;
}

function playerRight()
{
    $pEri.moveRight = true;
}

function playerLeftStop()
{   
    $pEri.moveLeft = false;
}

function playerRightStop()
{
    $pEri.moveRight = false;
}

function playerJump()
{
     if(!$pEri.airborne)
     {
          $pEri.setLinearVelocityY(-180);
          $pEri.airborne = true;
     }

}

function playerShoot()
{
    $pEri.shooting = true;
}

function playerClass::updateHorizontal(%this)
{
    if (%this.moveLeft) {
      %this.setLinearVelocityX( -$pEri.hSpeed );
    }

    if (%this.moveRight) {
       %this.setLinearVelocityX( $pEri.hSpeed );
    }

    if (!%this.moveLeft && !%this.moveRight) {
        %this.setLinearVelocityX( 0 );
    }

    if(!%this.airborne)
               %this.setLinearVelocityY(0);

}

function playerClass::updateVertical(%this)
{
     %yVelocity = %this.getLinearVelocityY();

     if(%this.airborne)
     {
          %this.setLinearVelocityY(0);
          %collision = %this.castCollision(0.005);

          if(%collision !$= "")
               %this.setLinearVelocityX(0);
     }

     %this.setLinearVelocityY(60);
     %collision = %this.castCollision(0.005);
     	
     if(%collision $= "")
     {
          %this.airborne = true;
          %this.setConstantForceY(70);
     }
     else if(%yVelocity < 0 && %this.airborne)
     {
          %this.setConstantForceY(70);
     }
     else
     {
          %this.airborne = false;
          %this.setConstantForceY(0);
     }

     %this.setLinearVelocityY(%yVelocity);

}

function playerClass::updateMovement(%this)
{

    %this.updateHorizontal();

    if(%this.airborne)
    %this.updateVertical();

    %this.updateAction();

    %this.setCurrentAnimation();

}

function playerClass::setCurrentAnimation(%this)
{
     %xVelocity = %this.getLinearVelocityX();
     %yVelocity = %this.getLinearVelocityY();

     if(%xVelocity > 0)
     {
          %this.setFlip(false, false);
     }
     else if(%xVelocity < 0)
     {
          %this.setFlip(true, false);
     }

     if(%this.airborne)
     {
        %this.playAnimation(EriJump);
     }
     else
     {


         if (%this.getLinearVelocityX() == 0)
         {  
            if(%this.getIsAnimationFinished())
            {
               %this.playAnimation(EriStand);
            }
         

            else
            {
                %this.playAnimation(EriStand);
            }
         }
            
         else
         {
            if(%this.getAnimationName() $= "EriRun")
            {
                if(%this.getIsAnimationFinished())
                {
                    %this.playAnimation(EriRun);
                }
            }

            else
            {
                %this.playAnimation(EriRun);
            }
         }

     }

}


function PlatformerSceneGraph::onUpdateScene()
{
     if (isObject($pEri))
          $pEri.updateMovement();
}

Appreciate any feedback on this. Thanks ^^

About the author

Recent Threads


#1
08/10/2009 (11:16 pm)
hello.

it seems that somehow, that tutorial has problems (you're not the 1st one comming here to ask how to resolve X problem, and showing off that SAME code).

so, here's my take on the problem...

function Player::move(%this, %dir)
{
   switch$(%dir)
   {
      case "up":  
         %this.setLinearVelocityY(-$pSpeed);
         %this.switchAnimation(up);
      case "down":
         %this.setLinearVelocityY($pSpeed);
         %this.switchAnimation(down);
      case "left":  
         %this.setLinearVelocityX(-$pSpeed);
         %this.switchAnimation(left);
      case "right":
         %this.setLinearVelocityX($pSpeed);
         %this.switchAnimation(right);
   }
}

the code above, simplifies the movement of the player (ud, down, left, right)... if you want it to jump, just add some code logic into it, and it should work.

you might have noticed the line "switchAnimation(%direction)", right?...

well, here's the code that does just that.
function Player::switchAnimation(%this, %dir)
{
   switch$(%dir)
   {
      case "up":
         %animation = "heli_top" @ %animation;
         %this.setAnimation(%animation);
        
      case "down":
         %animation = "heli_front" @ %animation;
         %this.setAnimation(%animation);
         
      case "left":
         %animation="heli_side" @ %animation;
         %this.setAnimation(%animation);
         %this.setFlipX(false);      // normal
        
      case "right":
         %animation="heli_side" @ %animation;
         %this.setAnimation(%animation);
         %this.setFlipX(true);      // derecha
         
      case "idle":
         %animation="heli_side" @ %animation;
         %this.setAnimation(%animation);
         %this.setFlipX(false);      // normal

      case "crash":
         return;     //por ahora, no haga nada.
      case "win":
         return;     //por ahora no hacer nada...
   }
}

as you can see, theres a section for every pose you want your char to react to... not just moving, but iddling and other stuff.

thats a simplified code i used on one of my games... but it should get you started on doing what you're trying to do.

PS: the problem with the code you psoted, is basically there are some functions lacking, and the updateAnimation funtcion its messed up.

my advice, if you wanna use code you found on books or tutorials, PLEASE try to understand it section by section... i mean, copy a portion of it, test it, understand what it does, rinse and repeat with some other functions... copying and pasting the whole code is no good, if you're not sure what each section does.
#2
08/11/2009 (12:31 am)
Ah, my apologies about posting this if it has been brought up and resolved already. The last I checked on the problems regarding this was a result of a buggy getIsAnimationFinished() function, but that was dated 2005. Thought they'd fix that by now >_<

I did modify the code actually to try and play around with checking if the animation is complete before replaying the animation, because I initially thought the animation being stuck on the first frame was due to the game logic repeatedly telling a sprite to play the same animation on every game loop.

I also tried using setting/getting flags to determine the animation state than the sprite should be in, rather than using velocity, since I thought the physics might be messing up the conditions, but the problem still remained.

I'll give your solution a shot and see how it goes. Thanks for the help and advice.
#3
08/11/2009 (12:57 pm)
not a problem.

yeah, i'd like to suggest that you stick with changing animations regardless if they are finished or not, if it works, then you can modify your code, so it tries to determine how to change the animation, given that the current animation is in a given frame... or something like that.