Game Development Community

Asteroids tutorial: displayLives behavior not behaving

by Ray Depew · in Torque Game Builder · 03/13/2008 (4:10 pm) · 2 replies

Somebody tell me if I did this right; if so, I'll edit the tutorial.

In the Asteroids tutorial, the "displayLives" behavior was acting screwy under 1.7.2. I found two problems, put a temporary patch on one, and fixed the other. I'll describe the fix first, and the patch afterwards.

1) The "update" function
"update" was supposed to display how many lives you had left (starting with 5), and take away one life icon every time you died. For me, "update" only took away a life the first time. It left the remaining 4 lives in the display, even after the final death.

Code in the tutorial:
function DisplayLivesBehavior::update(%this)
{
   %count = LifeIcons.getCount();
   
   for(%i=0;%i<%count;%i++)
   {
      %icon = LifeIcons.getObject(%i);
      %tempArray[%i] = %icon;
   }
   
   LifeIcons.clear();
   
   for(%i=0;%i<%count;%i++)
   {
      %tempArray[%i].safeDelete();
   }
   
   $currentLife--;
   
   if ($currentLife > 0)
   {
      %this.spawnCount = 0;
      %this.spawn();
   }
}

What I replaced it with, to get it to work:
function DisplayLivesBehavior::update(%this)
{
   %count = LifeIcons.getCount();
   
   %lastIcon = LifeIcons.getObject(%count - 1);
   LifeIcons.remove(%lastIcon);
   %lastIcon.safeDelete();
   
   $currentLife--;
}

I assume that much of the previous code was there to prevent memory leaks. Did my code do an adequate job of also preventing memory leaks?

2) Drawing the life icons in weird places

The "spawn" function used the method getAreaMin() to find the upper-left corner of the area where the life icons would be displayed (the "scoreDisplay" object). It called getAreaMin() five times (once for each life), extracted the X position from the result, and added the appropriate index to the X position, in order to find out where to place the icon. Icon 1 ended up at 0,0; Icon 2 ended up somewhere on the left-hand side of the display; Icon 3 ended up at 0,0 on top of Icon 1; Icon 4 was to the right of Icon 2 but a bit lower, and Icon 5 was positioned perfectly relative to Icon 4.

What I found out was that the statement
%xPos = getWord(%this.owner.getAreaMin(), 0) + modifier;
doesn't execute properly, and it causes some kind of malfunction in the next statement
%yPos = %this.owner.position.y;
. It returns bogus X and Y coordinates, unless you first look at the object with ... well, it's ugly and embarrassing, and it's not how I intended to fix it, but it worked. I inserted two diagnostic statements before getAreaMin(), and suddenly the icons started going where they were supposed to:
function DisplayLivesBehavior::spawn(%this)
{
...
   echo("position.X is ", %this.owner.position.x );
   echo("position.Y is ", %this.owner.position.y );
  %xPos = getWord(%this.owner.getAreaMin(), 0) + %modifier;
   %yPos = %this.owner.position.y
...
}

It's an ugly patch, but it works. I don't know why.

#1
03/15/2008 (3:54 am)
I have the original tutorial code working fine under 1.7.2. As a general question to start off with, are you copying and pasting the tutorial code from the web page into your script files or are you typing them out line by line?
#2
03/15/2008 (5:00 pm)
Yep - copied and pasted. I'll start over with a clean copy and see what happens.