There are not too many things wrong in chapter 8 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.