Game Development Community

Debugging movement code. Please take a look.

by Braedon Hinchcliffe · in Torque Game Builder · 12/27/2007 (4:16 pm) · 1 replies

Ingame I added dynamic fields to the player character.

WalkSpeed = 40
RunSpeed = 75
JumpHeight= 75

This is my movement code and it doesn't work anymore.
function playerClass::onLevelLoaded(%this, %scenegraph)
{
     $pGuy = %this;
      
      moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
      moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
      moveMap.bindCmd(keyboard, "space", "playerJump();", "");
     %this.setCollisionMaxIterations(2);
     %force = 20;
     sceneWindow2D.mount($pGuy, "0 0", %force, true);
}

function playerleft()
{
  $pGuy.moveleft = true;
  if(getSimTime() - $lastTimeRightPressed < 500)
  {
    $pGuy.moveSpeed = %this.RunSpeed;
  }
  else
  {
    $pGuy.moveSpeed = %this.WalkSpeed;
  }
  $lastTimeRightPressed = getSimTime();
}

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

function playerRight()
{
  $pGuy.moveRight = true;
  if(getSimTime() - $lastTimeLeftPressed < 500)
  {
    $pGuy.moveSpeed = %this.RunSpeed;
  }
  else
  {
    $pGuy.moveSpeed = %this.WalkSpeed;
  }
  $lastTimeLeftPressed = getSimTime();
}

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

function playerJump()
{
     if($lastTimeJumpPressed < 1)
     {
          $pGuy.setLinearVelocityY(%this.JumpHeight);
          //$pGuy.airborne = true;
     }
     else if(!$pGuy.airborne)
     {
          $pGuy.setLinearVelocityY(%this.JumpHeight);
          $pGuy.airborne = true;
     }
     $lastTimeJumpPressed = 1;
}

function playerClass::updateHorizontal(%this)
{
     if(%this.moveLeft)
     {
        %this.setLinearVelocityX(%this.moveSpeed);
     }

     if( %this.moveRight )
     {
        %this.setLinearVelocityX(%this.moveSpeed);
     }

     if(!%this.moveLeft && !%this.moveRight)
     {
          %this.setLinearVelocityX(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(100);
     %collision = %this.castCollision(0.005);
     	
     if(%collision $= "")
     {
          %this.airborne = true;
          %this.setConstantForceY(100);
     }
     else if(%yVelocity < 0 && %this.airborne)
     {
          %this.setConstantForceY(100);
     }
     else
     {
          %this.airborne = false;
          %this.setConstantForceY(0);
          $lastTimeJumpPressed = 0;
     }

     %this.setLinearVelocityY(%yVelocity);
} 


function playerClass::updateMovement(%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(playerJumpUp);
          }	
          else
          {
               %this.playAnimation(playerJumpDown);
          }
     }
     else
     {
if(%xVelocity == 0)
          {
               %this.playAnimation(playerStand);
          }
          else
          {
               if(%this.getAnimationName() $= "playerRun")
               {
                    if(%this.getIsAnimationFinished())
                    {
                         %this.playAnimation(playerRun);
                    }
               }
               else
               {
                    %this.playAnimation(playerRun);
               }
          }

     }

}

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

All I did was change pGuy.moveSpeed = 40 to %this.runspeed and so forth

#1
12/30/2007 (5:32 am)
I think your problem is that you are not passing in %this to your playerleft, etc functions... but trying to use it to retrieve the dynamic fields..