Here on this one page are all the fixes Tom and I have found for the book, “2D Game Building for Teens” by Michael Duggan.

Latest Update: September 26, 2009

The book, “2D Game Building for Teens” by Michael Duggan is the only book I’ve seen that covers Torque Game Builder — and since my son Tom is 15 I figured this book would be a great starting point for him.

Yes, and no. The book itself is very cool — but the code examples have some major problems.

Chapter 6 Fixes

My son spent hours getting to a point in the book where things just stopped working…

…the w, a, s, and d keys are supposed to move a flapping bat around on the screen.

Nada.

He spent hours trying to figure out what was wrong — he’s not a programmer (yet) so he had no idea what to look for. I finally found it after spending too long trying to find the typo in his code — turned out it’s a mistake in the book, he typed everything in correctly.

Hey, mistakes happen, right? So where’s the errata on the author or publisher site? It’s just freakin’ irresponsible not to have that available — especially for something that’s aimed at newbies.

So, for anybody else who comes across this problem, it’s caused by this line in player.cs:

$MeBatty = %this;

In that line you’re setting the global variable $MeBatty to the object that’s passed in to the function, but then other functions are referring to $BattyPlayer, not $MeBatty.

To fix player.cs (the full thing starts on the bottom of page 155 in 2D Game Building for Teens), just replace that $MeBatty line with this:

$BattyPlayer = %this;

Your bat should now fly around using the w, a, s, and d keys.


On pages 163 and 169 the book shows the code for player.cs — and besides the problem shown earlier, there is a huge chunk of the code shown that shouldn’t be there.

Basically, these functions…

BattyPlayerUp()
BattyPlayerDown()
BattyPlayerLeft()
BattyPlayerRight()

…are duplicated. You can’t have two functions in the same file using the same name.

The first set shouldn’t be there — just rip those four functions right out and you should be good to go.

Chapter 7 Fixes

When you create the MeanBat object, step 9 on page 191 references a coin, but it means bat. But the big thing is they don’t tell you to check the Callback checkbox in the World Limits rollout.

Without that the enemy bats will just keep flying instead of turning around and wreaking havoc (which is what we want).

Code problems — the code on page 194 (and repeated on page 196) has three problems — the wrong thing is the same in each case.

You’ll see reference to this in some of the lines:

%this.getRandom(%this.minSpeed, %this.maxSpeed)

That won’t work because there is no getRandom() function that’s part of the %this object — what should be there is this:

getRandom(%this.minSpeed, %this.maxSpeed)

That’s right, just knock off the %this. and you should be good. You’ll find that error in three different places in the code.

Also on those pages you’ll see a couple “case” statements, like this:

case "left"

You need to add a colon to the end of that:

case "left":

Do the same thing for the case “right” statement.

Chapter 8 Fixes

There are not too many things wrong in this chapter of 2D Game Building for Teens — but the things that are wrong will keep your game from working.

First, on page 218 the line of code that looks like this:

function BattyPlayer::createDeathray()

…should look like this:

function BattyPlayer::createDeathray(%this)

If you don’t pass in a reference to $BattyPlayer your deathray will never show up. That code is also shown again on page 222 at the top.

At the top of page 219, as part of the playerDeathray::fire function, the lines of code should be like this:

        %this.setWorldLimit(kill,"-50 -30 40 30");
	%this.setLinearVelocityX(%this.missileSpeed);
	%this.setPositionX(%this.player.getPositionX()+4);
	%this.setPositionY(%this.player.getPositionY()+7);
	%this.setImageMap(deathrayImageMap);
	%this.setSize("8 4");//approximately half size of original graphic
	%this.setCollisionActive(true,true);
	%this.setCollisionPhysics(false,false);
	%this.setCollisionCallback(true);

Some of those lines are the same as in the book, I just pasted the entire chunk here to make it easy for you to grab the whole block.

I did make a couple changes, however.

The book uses %this.setPosition(%this.player.getPosition()) which is fine if you want to set the deathray to the same position as Batty — but the raygun is supposed to be shooting, not the bat. So I used setPositionX and setPositionY so I could add an offset for each and make it look like the deathray was actually coming from the gun.

The other change is in the setSize line – the book said to set the values at 83 and 38, but for me that made a HUGE deathray, the size of half the screen. The values shown above fixed things.

Why that kind of change, after all, we’re only trying to set the size of a known graphic?

I’ll explain why in another post here later. =:)

That code above is also shown on page 222 as part of the entire listing.

One more thing, as in previous chapters, $BattyPlayer is mistakenly referred to as $MeBatty in the code on page 220 — make sure wherever you see $MeBatty you change it to $BattyPlayer.