Continuing Programming the Player Movement
The next section of code we are going to add is quite long but it will complete the movement portion of our character. Add this code to your text editor.



function MovementControlsBehavior::OnUpdate(%this)
{
if(%this.owner.getAnimationName() $= "Player_TakeDamage" || %this.owner.getAnimationName() $= "Player_Cast" || %this.owner.getAnimationName() $= "Player_Die")
{
%this.owner.setLinearVelocityX(0);
if(%this.owner.getIsAnimationFinished())
{
%this.owner.playAnimation(Player_Wait);
}}
else
{
if(%this.owner.getLinearVelocityY() < -1)
{
//This code makes the one way collisions
%this.owner.setCollisionActive(false,false);
}
else
{
%this.owner.setCollisionActive(true,true);
}
%this.owner.SetLinearVelocityX(%this.right - %this.left);
if(%this.up == 1)
{
if(!%this.owner.airborne)
{
%this.owner.setLinearVelocityY(-100);
%this.owner.airborne = true;
}}
%yVelocity = %this.owner.getLinearVelocityY();
if(%this.owner.airborne)
{
%this.owner.setLinearVelocityY(0);
%collision = %this.owner.castCollision(0.005);
if(%collision !$= "")
%this.owner.setLinearVelocityX(0);
}
%this.owner.setLinearVelocityY(70);
%collision = %this.owner.castCollision(0.005);
if(%collision $= "")
{
%this.owner.airborne = true;
%this.owner.setConstantForceY(70);
}
else if(%yVelocity < 0 && %this.owner.airborne)
{
%this.owner.setConstantForceY(70);
}
else
{
%this.owner.airborne = false;
%this.owner.setConstantForceY(0);
}
%this.owner.setLinearVelocityY(%yVelocity);
%this.Animation();
}}
function MovementControlsBehavior::Animation(%this)
{
%xVelocity = %this.owner.getLinearVelocityX();
%yVelocity = %this.owner.getLinearVelocityY();
if(%xVelocity > 0)
{
%this.owner.setFlip(false, false);
}
else if(%xVelocity < 0)
{
%this.owner.setFlip(true, false);
}
if(%this.owner.airborne)
{
if(%yVelocity != 0)
{
if(%this.owner.getAnimationName() $= "Player_Wait")
{
if(%this.owner.getIsAnimationFinished())
{
%this.owner.playAnimation(Player_Wait);
}}
else
{
%this.owner.playAnimation(Player_Wait);
}}}
else
{
if(%xVelocity == 0)
{
%this.owner.playAnimation(Player_Wait);
}
else
{
if(%this.owner.getAnimationName() $= "Player_Walk")
{
if(%this.owner.getIsAnimationFinished())
{
%this.owner.playAnimation(Player_Walk);
}}
else
{
%this.owner.playAnimation(Player_Walk);
}}}}
Save the file as PlayerMovement.cs in your behavior folder.
Go back to TGB and reload the project. We need to do this because we added a new behavior to our video game and Torque Game Builder needs to recognize it. After that is done add the behavior by going to the behavior field and press the green add button while Movement Controls is selected. Now you can playe your game.