Game Development Community

Platformer Tutorial

by David Taylor · in Torque Game Builder · 06/08/2008 (6:36 am) · 4 replies

Hi all.

I'm going through the platformer tutorial at the moment, and I'm trying to get my animations to work. They have been created in Torque, but I can't access them through my script. All I get is an error reading: "t2dAnimationController::playAnimation() - Invalid t2dAnimationDatablock datablock (playerStand)", etc.

So I know I'm getting to the setCurrentAnimation function - but it can't seem to locate the animations.

I suspect it's just a typo in my code, somewhere, but I've searched and searched and can't find the error. Can anybody help?

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)
   {
      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
   {
      %this.playAnimation(PlayerStandAnimation);
   }
}

#1
06/08/2008 (10:03 am)
I think you need to use %this.setAnimation() before you can call playAnimation();

Also, you should probably make your run animation looping, so you don't have to keep playing it every time it ends.
#2
06/08/2008 (5:22 pm)
Did you miss-name the playerStand animation when you created it in TGB? You say 'playerStand' in your comment, but 'PlayerStandAnimation' in the code... When doing call like this it is easy to miss spell something.
#3
06/11/2008 (11:37 pm)
Hi guys, thanks for the suggestions.

@ Conor, this code is a direct copy/paste from the tutorial, which is why I was surprised it didn't work. I already have %this.setCurrentAnimation(); in the onUpdate call. I even tried just putting:
%this.playAnimation(PlayerRunAnimation);
into the onLevelLoaded function, because I know that function is loading, but no response.

@ Steven, the animation is called PlayerStandAnimation in both the code and the animation itself.

Here's the full player class code. Any ideas, anyone?

function playerClass::onLevelLoaded(%this, %scenegraph)
{
     $player = %this;
      
      moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
      moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
      moveMap.bindCmd(keyboard, "up", "playerJump();", "");
      
      %this.enableUpdateCallback();
}

function playerClass::updateHorizontal(%this)
{
   if (%this.moveLeft && %this.moveRight)
   {
      %this.setLinearVelocityX(0);
      return;
   }
   
   if (%this.moveLeft)
   {
      if (!%this.againstLeftWall)
      {
         %this.againstRightWall = false;
         %this.setLinearVelocityX(-30);
      }
      return;
   }

   if (%this.moveRight)
   {
      if (!%this.againstRightWall)
      {
         %this.againstLeftWall = false;
         %this.setLinearVelocityX(30);
      }
      return;
   }

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

function playerClass::updateVertical(%this)
{
   %yVelocity = %this.getLinearVelocityY();
   
   %this.setLinearVelocityY(10);
   %collision = %this.castCollision(0.005);
   
   %normalX = getWord(%collision, 4);
   %normalY = getWord(%collision, 5);
   
   // no collision
   if (%collision $= "")
   {
      %this.airborne = true;
      %this.setConstantForceY(100);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // collides with wall to the left
   if (%normalX == 1 && %normalY == 0)
   {
      %this.againstLeftWall = true;
      %this.setLinearVelocityX(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // collides with wall to the right
   if (%normalX == -1 && %normalY == 0)
   {
      %this.againstRightWall = true;
      %this.setLinearVelocityX(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // on ground with no wall collisions
   if (%normalX == 0 && %normalY == -1)
   {
      %this.airborne = false;
      %this.againstLeftWall = false;
      %this.againstRightWall = false;
      %this.setConstantForceY(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // in air and hits platform with head
   if (%normalY == 1)
   {
      %this.airborne = true;
      %this.setLinearVelocityX(0);
      %this.setConstantForceY(100);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // in case another type of collision normal was detected
   error("another collison type" SPC %normalX SPC %normalY);
   %this.airborne = false;
   %this.againstLeftWall = false;
   %this.againstRightWall = false;
   %this.setLinearVelocityY(%yVelocity);
}

function playerClass::onUpdate(%this)
{
   %this.updateHorizontal();
   %this.updateVertical();
   %this.setCurrentAnimation();
}

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

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

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

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

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

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)
   {
      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
   {
      %this.playAnimation(PlayerStandAnimation);
   }
}
#4
06/11/2008 (11:43 pm)
Okay, I just figured it out, and I feel pretty sheepish! I must have clicked on something else as playerClass, and not the player. I looked at the scripting and it had no class name, haha! So now I have animation! (And a whole myriad of other problems!) :)

Thanks, guys! :)