Game Development Community

Multiple Characters (Torque 2d)

by Eric Nelson · in Game Design and Creative Issues · 04/05/2012 (11:48 pm) · 4 replies

Ok, the game I am working on is supposed to be a 2d pvp fighting game. I have two player classes set up. I know the code works right because when I just have one class loaded the character moves and jumps just fine. However when I add the second character class the second character moves but not the first. Both characters in the editor have unique names and classes, and each class file has individual keybinds. I did notice something peculiar. For movement, I had both characters call the same named functions (moveLeft, moveRight, jump) which had the keybinds for player 1 affect the movement of player two, and not the other way around. In other languages, if an object or another class each had a function with the same name they would still do two separate things. Now, when add "Two" to the name of the previous functions I listed, my keybinds for player two work properly, but then player one doesn't actually move. It can look right and left, but not move right and left. He can jump, but once he lands he can't jump again. If I only have player one loaded then it works perfectly fine normally.

I'm not sure what the problem is right now and not sure what code I should show when the two are basically identical.

#1
04/06/2012 (12:27 pm)
The first thing I would want to see is your class function definitions of your different classes.

function AIOne::updateMoveToward(%this, %pos)
{...

function AITwo::updateMoveToward(%this, %pos)
{...
#2
04/09/2012 (12:38 pm)
function playerClass::onLevelLoaded(%this, %scenegraph)
{
     $pGuy = %this;
      
      moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
      moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
      moveMap.bindCmd(keyboard, "space", "playerJump();", "");
	  moveMap.bindCmd(keyboard, "z", "$pGuy.createStar();", "");
	  moveMap.bindCmd(keyboard, "c", "$pGuy.Melee();", "");
	  
	  %this.enableUpdateCallback();
}

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()
{
     if(!$pGuy.airborne)
     {
          $pGuy.setLinearVelocityY(-150);
          $pGuy.airborne = true;
     }
}
function playerClass::updateMovement(%this)
{ 
     if(%this.moveLeft)
     {
            %this.setLinearVelocityX(-60);
     }
    
     if(%this.moveRight)
     {
            %this.setLinearVelocityX(60);
     }
    
     if(!%this.moveLeft && !%this.moveRight)
     {
            %this.setLinearVelocityX(0);
     }

   
    $pGuy.setCurrentAnimation(%yVelocity);
	%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::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);
    }
    }
}

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

function playerClass::createStar(%this)
{
   %this.ninjaStar = new t2dStaticSprite()
   {
      scenegraph = %this.scenegraph;
      class = ninjaStar;
      projectile=%this.projectile;
      player = %this;
   };
   %this.ninjaStar.fire();
}

function playerClass::Melee(%this)
{
   %this.ninjaMelee = new t2dStaticSprite()
   {
      scenegraph = %this.scenegraph;
      class = ninjaMelee;
      projectile=%this.projectile;
      player = %this;
   };
   %this.ninjaMelee.fire();
}
#3
04/09/2012 (12:39 pm)
function playerClassTwo::onLevelLoaded(%this, %scenegraph)
{
     $pRob = %this;
      
      moveMap.bindCmd(keyboard, "j", "playerTwoLeft();", "playerTwoLeftStop();");
      moveMap.bindCmd(keyboard, "l", "playerTwoRight();", "playerTwoRightStop();");
      moveMap.bindCmd(keyboard, "i", "playerTwoJump();", "");
	  //moveMap.bindCmd(keyboard, "z", "$pRob.createStar();", "");
	  //moveMap.bindCmd(keyboard, "c", "$pRob.Melee();", "");
	  
	  %this.enableUpdateCallback();
}

function playerTwoLeft()
{
    $pRob.moveLeft = true;
    $pRob.setFlip(true, false);
}

function playerTwoLeftStop()
{
    $pRob.moveLeft = false;
}

function playerTwoRight()
{
     $pRob.moveRight = true;
     $pRob.setFlip(false, false);
}

function playerTwoRightStop()
{
     $pRob.moveRight = false;
}

function playerTwoJump()
{
     if(!$pRob.airborne)
     {
          $pRob.setLinearVelocityY(-150);
          $pRob.airborne = true;
     }
}
function playerClassTwo::updateTwoMovement(%this)
{ 
     if(%this.moveLeft)
     {
            %this.setLinearVelocityX(-60);
     }
    
     if(%this.moveRight)
     {
            %this.setLinearVelocityX(60);
     }
    
     if(!%this.moveLeft && !%this.moveRight)
     {
            %this.setLinearVelocityX(0);
     }

   
    $pRob.setCurrentAnimation(%yVelocity);
	%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 playerClassTwo::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);
    }
    }
}

function t2dSceneGraph::onUpdateScene()
{
       $pRob.updateTwoMovement();
}
function playerClassTwo::explode(%this)
{
   %this.safeDelete();
}
#4
04/10/2012 (7:55 pm)
Ah!

Instead of

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

it should be

function playerClass::onUpdate()  
{  
       $pGuy.updateMovement();  
}

and instead of

function t2dSceneGraph::onUpdateScene()  
{  
       $pRob.updateTwoMovement();  
}

it should be

function playerClassTwo::onUpdate()  
{  
       $pRob.updateTwoMovement();  
}


What you had before, I think, was you were re-defining what the function "t2dSceneGraph::onUpdateScene()" did instead of doing your code once for each object.

I could be wrong, though, so make sure you save before making these changes.