Game Development Community

Mini Platformer movement problem

by Tomas · in Torque Game Builder · 10/10/2009 (5:35 pm) · 10 replies

Hi I just finished the Mini platformer tutorial on TDN but when I test ran it my char. just stayed in the same spot I put him in the level editor suspended over the first platform. Now I double checked my code and its exactly like the one in the tut. And even though I used my own sprites I named everything exactly as if I used the ninja sprite. Im currently using the newest version of TGB if that would make a difference on this code. And if it doesn't and the code posted bellow doesn't have anything wrong with it what would be the thing to look for in the Level builder that would cause this. And I made sure to put the
exec("./playerClass.cs"); in the game.cs.

player.cs code:

function playerClass::onLevelLoaded(%this, %scenegraph)
{
$player = %this;

moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
moveMap.bindCmd(keyboard, "space", "playerJump();", "");

%this.enableUpdateCallback();
}
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::updateHorizontal(%this)
{
if (%this.moveLeft == %this.moveRight)
{
%this.setLinearVelocityX(0);
return;
}

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

if (%this.moveRight)
{
if (!%this.againstRightWall)
{
%this.againstLeftWall = false;
%this.setLinearVelocityX(30);
}
}
}
///////////////////////////////////////////////
function playerClass::updateVertical(%this)
{
%yVelocity = %this.getLinearVelocityY();

%this.setLinearVelocityY(5);
%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();
}
////Animation
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
10/10/2009 (8:05 pm)
You should put your code inbetween "(code)" and "(/code)", but using square brackets instead of parentheses (you can learn more in the MarkupLite link above the reply box).

Anyway... I don't see a "moveMap.push();" anywhere in there. Without that, the keybinds you've set up won't be active yet.

At least we can hope that it's as simple as that! *KNOCK ON WOOD!*
#2
10/10/2009 (9:05 pm)
Thanks for the reply but I re examined the code in the tutorial and could not find any mention of "moveMap.push();" and Im convinced now that my code is exactly what the tutorial has.
I did try putting the "moveMap.push();" under line 6 but it didn't do anything. I also relooked at all the settings needed in the levelbuilder and there I dentical to the tutorial as well.
#3
10/10/2009 (11:46 pm)
The next thing to check is to see if the "Class" attribute is set for your sprite. It should be set to "playerClass" so that the "onLevelLoaded" function will work.
#4
10/11/2009 (11:15 am)
The Class is set to playerClass and the Name set to player.
In the collision drop down I have Send Collisions checked, Send Physics checked, Detection Mode set to polygonal (I have a costume polygon made exactly like the one in the tut.), I have circle scale set to 1.000, Collision response set to CLAMP, Collision Layers set to All, and collision groups set to 1.

In the Physics drop down I have Gravity checked and set to 4.000, and I have Calculate Mass and inertia checked with the following stats:
Mass 0.079
Inertia 0.153
Density 0.010
Damping 0.000
Friction 0.300
Restitution 1.000
And I tried it with Calculate Mass and Inertia checked and unchecked since the tutorial didn't mention it.
#5
10/11/2009 (2:30 pm)
At this point, I'd put echos into a few araes: playerLeft, playerRight, and onUpdate. Check the console to make sure all of those are being called.

If they are all being called, and you STILL aren't moving, then I'd recommend making a ZIP of your project and putting a link here. I can probably take a glance at it tonight.
#6
10/11/2009 (5:10 pm)
Ok so I completely redid the tutorial from scratch and found that I named my player script player.cs instead of playerClass.cs and I for got to set my player image to group 1. After doing that now when I run it I see my tilemap but my character is missing? And not to take any chances this second time around I copy and pasted the code directly for the tutorial.

function playerClass::onLevelLoaded(%this, %scenegraph)
{
     $player = %this;
      
      moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
      moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
      moveMap.bindCmd(keyboard, "space", "playerJump();", "");
      
      %this.enableUpdateCallback();
}
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::updateHorizontal(%this)
{
   if (%this.moveLeft == %this.moveRight)
   {
      %this.setLinearVelocityX(0);
      return;
   }
   
   if (%this.moveLeft)
   {
      if (!%this.againstLeftWall)
      {
         %this.againstRightWall = false;
         %this.setLinearVelocityX(-30);
      }
   }

   if (%this.moveRight)
   {
      if (!%this.againstRightWall)
      {
         %this.againstLeftWall = false;
         %this.setLinearVelocityX(30);
      }
   }
}
function playerClass::updateVertical(%this)
{
   %yVelocity = %this.getLinearVelocityY();
   
   %this.setLinearVelocityY(5);
   %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 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);
   }
}
#7
10/11/2009 (5:15 pm)
Sorry for the code again I read the MarkupLite thing but I apparently did something wrong.
#8
10/12/2009 (12:11 am)
Modify the code post and change your brackets to the following

{code}
Your code here
{/code}

Of course, using square brackets instead of curly braces. It'll make it a lot easy to see.
#9
10/12/2009 (12:12 am)
Also, make sure you are calling exec on the playerClass.cs file somewhere else.
#10
10/12/2009 (5:53 pm)
Im calling in the game.cs script with this:

exec("./playerClass.cs");

Im putting it right bellow this:

function startGame(%level)
{