Game Development Community

Ninja Platformer Help.

by Ian Willbond · in Torque Game Builder · 03/14/2012 (8:27 pm) · 7 replies

Hi, I've been trying to learn how to use TGB and thought I would give some tutorials a go. I started on Ninja Platformer and followed it to the letter, but am having issues with the character jumping, it doesn't. I increased the castCollision to 1.0 which allows the player to jump but then the jumping and movement just go a bit nuts.

I've added the code for the tutorial below, any help would be appreciated

Thanks.

Ian.

function playerClass::onLevelLoaded(%this, %scenegraph)
{
     $pGuy = %this;
      
      moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
      moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
      moveMap.bindCmd(keyboard, "space", "playerJump();", "");
      $pGuy.setCollisionMaxIterations(2);
      %force = 20;
      sceneWindow2D.mount($pGuy, "0 0", %force, true);
}

function playerLeft()
{
    $pGuy.moveLeft = true;
    $pGuy.setFlip(true, false);
}

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

function playerRight()
{
     $pGuy.moveRight = true;
     $pGuy.setFlip(false, false);
}

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

function playerJump()
{
	%yVelocity = $pGuy.getLinearVelocityY();
	%xVelocity = $pGuy.getLinearVelocityX();

	$pGuy.setLinearVelocityY(100);
	$pGuy.setLinearVelocityX(0);

  	%collision = $pGuy.castCollision(0.005);
    
	if(!(%collision $= ""))
	{
		$pGuy.setLinearVelocityY(-225);
	}
	else
	{
		$pGuy.setLinearVelocityY(%yVelocity);
	}

	$pGuy.setLinearVelocityX(%xVelocity);
}


function playerClass::updateMovement(%this)
{ 
     if(%this.moveLeft)
     {
            %this.setLinearVelocityX(-225);
     }
    
     if(%this.moveRight)
     {
            %this.setLinearVelocityX(225);
     }
    
     if(!%this.moveLeft && !%this.moveRight)
     {
            %this.setLinearVelocityX(0);
     }
     
   
     %yVelocity = $pGuy.getLinearVelocityY();
     $pGuy.setCurrentAnimation(%yVelocity);
                
    $pGuy.setLinearVelocityY(100);
  
    %collision = $pGuy.castCollision(0.005);
    $pGuy.setLinearVelocityY(%yVelocity );
    
    if(%collision $= "")
    {
        $pGuy.setConstantForceY(100);
    }
    else
    {
        $pGuy.setConstantForceY(0);
    } 
}


function t2dSceneGraph::onUpdateScene()
{
       $pGuy.updateMovement();
}


function playerClass::setCurrentAnimation(%this, %yVelocity)
{
   
   if(%yVelocity < 0 )
    {
    	 %this.playAnimation(playerJumpUp);
    }
    else if(%yVelocity > 0 )
    {
    	 %this.playAnimation(playerJumpDown);
    }
    else
    {

       if(%this.moveLeft || %this.moveRight)
       {
           if(%this.getAnimationName() $= "playerRun")
           {
               if(%this.getIsAnimationFinished())
               {
                   %this.playAnimation(playerRun);
               }
           }
           else
           {
               %this.playAnimation(playerRun);
           }
       }   
       else
       {
           %this.playAnimation(playerStand);
       }
    
    }
}

#1
03/14/2012 (9:18 pm)
Try changing $pGuy.setLinearVelocityY(100) on line 41 to $pGuy.setLinearVelocityY(10). I've made a lot of other changes to my version of that project, so it might be that it won't help.
#2
03/14/2012 (10:40 pm)
Wow thanks so much James that worked perfectly!
#3
03/15/2012 (1:03 pm)
Awesome, I'm glad that helped!
#4
05/07/2012 (4:24 am)
Thanks for that response, I've been dealing with the same problem myself and that solved it!
#5
05/07/2012 (10:22 am)
I just ran through the Ninja Platformer tutorial and I had some issues with getting my dude off the ground as for some reason "$pGuy.castCollision(0.005)" was returning a blank value when he was just standing still on a tile. Jumping seems to be mostly working now but every so often I'm still not getting a value returned when standing still and I don't know why.

Also there are two other kind of strange things going on that I'm not sure how to approach.

1) When moving left or right against a vertical tile the player's sprite kind of bounces off of it a little bit so when rubbing against something you get this strange jerky bounceback that looks really bad. Based on the tutorial I thought that's what "$pGuy.setCollisionMaxIterations(2);" was supposed to fix but I suppose not?

2) When you jump if you're rubbing up against a vertical surface you'll just travel up forever. I know this is because "castCollision" is returning a collision value but what's the best way to parse that value out? I imagine that you'd want to see where the collision is coming from and stop vertical linear velocity if there's no collision only from below.

If anyone can lend a hand it'd be greatly appreciated.
#6
07/24/2012 (6:38 am)
I am having the same problems as Chris. Both points 1 and 2 in his post, and cant figure them out. Any ideas?
#7
07/24/2012 (6:55 am)
I actually never figured it out but I have a feeling that whatever collision detection is going on in that tutorial is just completely off and not the way to handle things at all. I've since moved on to messing with the Platformer Kit Pro and collision is handled much, much better there.