Sync Tutorial - Programming Player's Death and Spawn
Now we are going to work on what the Player's Ship will do after it runs into an enemy.
Open your ShipMovement.cs and add the following code.
function PlayerClass::Explode(%this)
{
//add explosion particle effect
%this.setLayer(31);
%
this.disableUpdateCallback();
%
this.setCollisionActive(false, false);
//the player's ship goes behind the backgrouind and can't move untill respon.
Trigger1.setCollisionActive(true, true); //sends an explode command to enemies

%this.lives += -1;
if(%this.lives > 0)
{
%
this.schedule(1500, "spawn");
}
}

function PlayerClass::Spawn(%
this)
{
%
this.setCollisionActive(true, true);
%
this.setLayer(5);
%
this.setPosition(0,0);
}
Now when the player’s ship collides with the enemy ship they both die. Then if the player has enough lives the player will respawn.
Add this code to the function ShipMovementBehavior::onBehaviorAdd(%this).
%this.owner.lives = 5;
The player will now respawn.

It is annoying to respawn to a bunch of enemies attacking you so we will make spawn protection for the player.
Replace the function PlayerClass::Spawn(%
this) with this code.
function PlayerClass::Spawn(%this)
{
%
this.schedule(1500,"spawnProtectionOff");
%
this.setCollisionActive(false, false);
%
this.setLayer(5);
%
this.setPosition(0,0);
}
Now add this code to the bottom of the ShipMovement.cs.
function PlayerClass::SpawnProtectionOff(%this)
{
%
this.setCollisionActive(true, true);
}
The player now has 1.5 second spawn protection.
Next Page
Previous Page