Continuing Making the Player

We now need to make a collision so the player doesn't fall through the ground when you play the game. Select the old man and click on the the edit this objects collision polygon button in the upper left part of the selection. Move the vertices so they look like the picture to the right. Make sure the collision is flat at the bottom so the player can walk smoothly along the ground.
Torque Game Builder Collisions
The collision should look like this
Go to the edit tab and check send and recieve collision.
Making Platform Collisions

Open up the Collision.png and drag it into the game. Put the image over a platform and stretch the X-axis so it covers the platform. Change the layer to 31 and set its send and receive collision on. Check the immovable state under physics. Repeat this until all the platforms are filled.
The platforms should look like this
Programming the Player Movement

Open up your text editor. Note you can't use a word processor because you have to save the file as a .cs. Add the following code to your text editor:
if (!isObject(MovementControlsBehavior))
{
   %template =
new BehaviorTemplate(MovementControlsBehavior);
  
   %template.friendlyName =
"Movement Controls";
   %template.behaviorType =
"Input";
   %template.description  =
"Platformer style movement control";
  
   %template.addBehaviorField(upKey,
"Key to bind to Jump", keybind, "keyboard up");
   %template.addBehaviorField(leftKey,
"Key to bind to Run left", keybind, "keyboard left");
   %template.addBehaviorField(rightKey,
"Key to bind to Run right", keybind, "keyboard right");
   %template.addBehaviorField(space,
"Key to bind to Cast Spell", keybind, "keyboard space");
   %template.addBehaviorField(a,
"Key to bind to Change Spell Left", keybind, "a");

   %template.addBehaviorField(Health,
"Health Stat", float, 50.0);
   %template.addBehaviorField(Defense,
"Defense Stat", float, 0.0);
}

function MovementControlsBehavior::onBehaviorAdd(%
this)
{
if (!isObject(moveMap))
return;
%
this.owner.enableUpdateCallback();  
%
this.owner.Health = %this.health;
%
this.owner.Defense = %this.defense;
$Spell = 1;
%
this.owner.spellAble = true;

moveMap.bindObj(getWord(%
this.upKey, 0), getWord(%this.upKey, 1), "MoveUp", %this);
moveMap.bindObj(getWord(%
this.leftKey, 0), getWord(%this.leftKey, 1), "MoveLeft", %this);
moveMap.bindObj(getWord(%
this.rightKey, 0), getWord(%this.rightKey, 1), "MoveRight", %this);
moveMap.bindObj(getWord(%
this.space, 0), getWord(%this.space, 1), "PressSpace", %this);
moveMap.bindObj(getWord(%
this.a, 0), getWord(%this.a, 1), "PressA", %this);
}

function MovementControlsBehavior::onBehaviorRemove(%
this)
{
if (!isObject(moveMap))
return;

%
this.owner.disableUpdateCallback();
 
moveMap.unbindObj(getWord(%
this.upKey, 0), getWord(%this.upKey, 1), %this);
moveMap.unbindObj(getWord(%
this.leftKey, 0), getWord(%this.leftKey, 1), %this);
moveMap.unbindObj(getWord(%
this.rightKey, 0), getWord(%this.rightKey, 1), %this);
  
%
this.up = 0;
%
this.left = 0;
%
this.right = 0;
%
this.space = 0;
}

function MovementControlsBehavior::moveUp(%
this, %val)
{
   %
this.up = %val;
}

function MovementControlsBehavior::moveLeft(%
this, %val)
{
if(%val < 0.20 && %val > -0.20) //used with joystick
{
%val = 0;
}
   %
this.left = %val * 28; // makes the playerfs horizontal speed to 28.
}

function MovementControlsBehavior::moveRight(%
this, %val)
{

if
(%val < 0.20 && %val > -0.20) //used with joystick
{
%val = 0;
}
   %
this.right = %val * 28;// makes the playerfs horizontal speed to 28.
}

function MovementControlsBehavior::PressSpace(%
this, %val)
{
if(%val == 1)
{
%
this.space = %val;
}}

This code sets up a behavior complete with functions that set variables like %this.left to when a button is pressed. Also the line that says %this.owner.enableUpdateCallback(); creates a callback that the computer runs through every second. We will utilize this updateCallback to make the player move.
Previous Page
Next Page