TGB/Tutorials/Asteroids/Section3

From TDN


Asteroids Tutorial

Written for TGB Version: 1.6


Section 3

Thanks to behaviors, a simple yet effective way to add a scoring mechanic will be introduced.


Adding Behaviors


With the text editor of your choice, create the following script files and save them to your behavior folder.

displayScore.cs

//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// Behavior by Mike Lilligreen for TDN Asteroids Tutorial
//-----------------------------------------------------------------------------

if (!isObject(DisplayScoreBehavior))
{
   %template = new BehaviorTemplate(DisplayScoreBehavior);
   
   %template.friendlyName = "Display Score";
   %template.behaviorType = "GUI";
   %template.description  = "Allows a text object to display the score";
}

function DisplayScoreBehavior::onAddToScene(%this, %scenegraph)
{
   $currentScore = 0;
   %this.owner.text = "Score:" SPC $currentScore;
}

function DisplayScoreBehavior::updateScore(%this)
{
   %this.owner.text = "Score:" SPC $currentScore;
}


scorePoints.cs

//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// Behavior by Mike Lilligreen for TDN Asteroids Tutorial
//-----------------------------------------------------------------------------

if (!isObject(ScorePointsBehavior))
{
   %template = new BehaviorTemplate(ScorePointsBehavior);
   
   %template.friendlyName = "Score Points";
   %template.behaviorType = "Game";
   %template.description  = "Gives the object a point value for scoring purposes";

   %template.addBehaviorField(pointValue, "How much the object is worth", int, 10);
   %template.addBehaviorField(counter, "The score counter object", object, "", t2dSceneObject);
}

function ScorePointsBehavior::onRemove(%this)
{
   %counter = %this.counter.getBehavior("DisplayScoreBehavior");
   if (!isObject(%counter))
      return;
      
   $currentScore = $currentScore + %this.pointValue;
   %counter.updateScore();
}

Now with these behaviors, start up TGB.

Create a Score GUI Element


As you can guess, the scoring system has two pieces. The first piece is an object that will let the player know what the current score is. On the create tab, find the t2dTextObject and drag one out into your scene. Type the following text:

Score:

Place the object wherever you wish, for this tutorial it is in the upper left hand corner of the camera view.

Image:TGB Asteroids Tutorial 3 1.jpg

The properties for this object are below:

Image:TGB Asteroids Tutorial 3 2.jpg Image:TGB Asteroids Tutorial 3 3.jpg
Image:TGB Asteroids Tutorial 3 4.jpg


Give the Asteroids Some Points


With the other behavior we've created, scorePoints, add this to both the large and small asteroid. Let the asteroids know what the score counter object is.

Image:TGB Asteroids Tutorial 3 5.jpg

The point values are of course totally flexible. In this tutorial the large asteroids are worth 10 points and the small ones 20.

With this now set up, play the scene and rack up the points!

One note: If the score text does not look sharp, you can edit this by going into the .t2d file of your level and changing the "fontSizes" property of the t2dTextObject. To find the right fontSize, take your display resolution and divide it by the camera size. In this tutorial, the display resolution is 1024x768 and the camera size is 100x75. 1024/100 and 768/75 both are 10.24. Take the character height of 3 and multiply that by 10.24 and you get 30.72 which is rounded to the fontSize of 31. Do that for your resolution and camera size and it should look right.

Image:TGB Asteroids Tutorial 3 6.jpg


We now have one interface element set up to let the player know the score. In the next section, we'll create an GUI element to represent the player's lives.


Return to Asteroids Tutorial Hub