Sync Tutorial - Further Programming Player Movement
Now we are going to further edit our ShipMovement.cs.
Add this code into your Ship Movement.cs.
function ShipMovementBehavior::onBehaviorAdd(%this)
{
if (!isObject(moveMap))
return;
%this.owner.enableUpdateCallback();
%this.owner.setDamping(%this.damping);
moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
moveMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %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);
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
}
function ShipMovementBehavior::onBehaviorRemove(%this)
{
if (!isObject(moveMap))
return;
%this.owner.disableUpdateCallback();
moveMap.unbindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), %this);
moveMap.unbindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 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.down = 0;
%this.left = 0;
%this.right = 0;
}
function ShipMovementBehavior::moveUp(%this, %val)
{
%this.up = %val;
}
function ShipMovementBehavior::moveDown(%this, %val)
{
%this.down = %val;
}
function ShipMovementBehavior::moveLeft(%this, %val)
{
%this.left = %val;
}
function ShipMovementBehavior::moveRight(%this, %val)
{
%this.right = %val;
}
This code sets variables in the game engine which happen when the player presses a key. We are going to use these variables to tell the player’s ship what to do.