Scores are an essential part in all games. They challenge the user while rewarding them at the same time. Keeping track of scores in Flash isn't tricky to use, but it is important that they are implemented correctly.
Declaring The Variable
The fundamental building block behind a score is the variable. As you ought to have learned, variables are used to store objects, pointers, and raw data. If you haven't, you only need to know that a variable will keep track of the player's score for us. This is what a score variable will look like when it is declared:
var score:Number = 0;
Though it should be evident, this line makes a new variable that can be accesed by 'score' and holds a value of zero. Typically, scores are numbers and thus the above is a strict declaration of Number meaning it can only hold a number (floats and intgers).
Note that where you place this line of code is important. Say if you have a frame for every level and put that on every frame, the score will be erased and reset to zero at a new place in memory. But perhaps you wanted the score to continue incrementing from level to level, then you would need to declare the score variable before the level frames. In most cases you should declare the variable when the movie is initiated. In AS3, it is best to put it in the the document class; in AS2, the first frame. This way the variable can only be modified and not re-declared.
Changing The Score
Now that there is a variable in place to keep track of the player's score, the variable must be updated accordingly. This is done with simple arithmetic and assignment operators:
+, -, *, /, %, +=, -=, *=, /=, %=, ++, and --
When implemented, a typical line should look like this:
score += 5;
This might have to be changed depending on the namespace of score. If it was declared as recommended (Main Timeline), it should always be called directly:
AS2:
_root.score += 5;
AS3:
MovieClip(root).score += 5;
Say if you call score += 5 from an instace of an enemy, it will try accessing an imaginary (unless created for some other reason) variable in the enemy object that does not exist. Thus being the reason the score variable is global, or at the lowest level of the internal namespace. (Unless you're anal about security and create an object for the game which has a variable with get() and set() functions)
Displaying The Score
Displaying the score is a quick process once you've got it down. To start, add a text box to the stage in the place you would like your score to be shown. Select the new box (it should be already) and find the poperties window (Ctrl+F3). Now there will be two different proccesses depending on the version of actionscript your in.
AS2:
Change the text box's Text Type to Dynamic Text.
Change the Var to your score variable. (It's good to keep it on the main stage as mentioned, use _root if you did)
And that's it! Your the score will be displayed exactly as it is in memory.
AS3:
Change the text box's Text Type to Dynamic Text.
Give it an instance name (ie. txtBx)
Create an onFrame event and use 'MovieClip(root).scrBx.text = String(MovieClip(root).score).' Or to be more efficient, just use it when the score is updated.
Obviously it takes a bit more work to display the score in AS3, so to cut down a bit, it might be easier to create a function that will be called by some onFrame event running during the game.
public function changeScore(num:Number):void{
MovieClip(root).score += num;
MovieClip(root).scrBx.text = String(MovieClip(root).score);
}