Scoreboard Hints
Trying to add a scoreboard feature to your Invaders game? Here are some potentially helpful hints.
I. What object keeps score?
In the given code, the natural place to increment score comes in the class for the invaders (invaderClass.as), where we hit test against the bullet.
Note, however, that if you define the variable that keeps score within invaderClass, each invader will be keeping its own score! That's because variables defined within instances of an object are local to the instance, not common to the whole system; and this is true even when all those instances are running the same code.
So you'll need to keep score on some object of which there is only a single instance: the shooter, for instance, or the bullet (unless you're going for multiple shots).
Alternatively, you could define and manage your scoring variable by adding a document class.
In either case, you'll need each of your invader instances to increment the score-keeping variable wherever you've installed it. This will involve MovieClip(root).
If you're using shooterClass to keep score, you'll be saying something like:
MovieClip(root).shooter.score ++;
If you're introducing a document class and keeping score there, you'll say something like:
MovieClip(root).score ++;
II. Putting score into a text field
Here's something you CANNOT say:
MovieClip(root).scoreField.text = score ++;
The example above assumes there is a Text Field with the instance name scoreField in the main movie. That much may be correct, but there are two things wrong with the statement.
First, you can't mathematically increment a number displayed in a Text Field. That's because (second problem) what's displayed in a Text Field can only ever be a String. String objects (series of alphanumeric characters) cannot be modified mathematically.
Here's aother crucial consequence of this fact: You cannot assign a Number variable to the text property of a Text Field. So if I declare a score-keeping variable as follows:
var myScore:Number = 0;
And then write:
scoreField.text = myScore;
I'll get an error message complaining that I am trying to coerce Number data into a String. (This was legal in ActionScript 2, but it's now forbidden.)
The solution lies in our old fiend, the conversion function. The following statement will work:
scoreField.text = String(myScore);
III. Updating the score
Finally, you'll need to think about how the scoreboard is refreshed. Should it happen automatically on frame entry? In that case, you'll want an ENTER_FRAME listener and callback on whatever object is keeping score.
On the other hand, you could have the score updated only when it changes. You might do this by defining a custom method in your scorekeeping object, and calling that method when the invaders are hit by a bullet. Remember that calling a method on another object also uses our ubiquitous root technique:
MovieClip(root).someObject.doSomething();
|
|