Sync Tutorial - Programming Enemies
Next Page
Make a new file in your text editor such as notepad.
DO NOT USE WORD PROSSESSORS.
Save the file as EnemyA.cs in the behavior folder of your game.
Add the following code to your new EnemyA.cs file.
if (!isObject(EnemyABehavior))
{
%template = new BehaviorTemplate(EnemyABehavior);
%template.friendlyName = "EnemyA";
%template.behaviorType = "AI";
%template.description = "The enemy moves towards the playerShip";
%template.addBehaviorField(acceleration, "Forward acceleration", float, 10);
%template.addBehaviorField(turnSpeed, "Velocity of turning)", float, 600);
%template.addBehaviorField(damping, "Amount to damp movement", float, 5);
}
function EnemyABehavior::onBehaviorAdd(%this)
{
if (!isObject(moveMap))
return;
%this.owner.enableUpdateCallback();
%this.owner.setDamping(%this.damping);
%this.owner.setlayer(8);
}
function EnemyABehavior::onBehaviorRemove(%this)
{
if (!isObject(moveMap))
return;
%this.owner.disableUpdateCallback();
}
Go back into Torque Game Builder and go to project, reload project. You need to reload the project every time you add a new behavior.
Select the enemy and add the EnemyA behavior.
Now we are going to continue editing our Enemy Code.
Add the following code to your EnemyA.cs file.
function EnemyClassA::OnLaserHit(%this)
{
%this.health += 1;
if(%this.health == 4)
{
%this.Explode();
}
else
{
%this.setFrame(%this.health);
}
}
function EnemyClassA::Explode(%this)
{
//play explode particle effect
%this.safeDelete();
}
Run your game and shoot at enemyA. The enemy should loose the blue bars and die on the fourth hit.