So you are almost done with your new Flash game, and you test it out and realize it is missing one rather important aspect: score. This tutorial will teach you how to add that important aspect to your game.
First of all, you want to make a variable for the score. You can think of a variable as a box that stores a value. You can access this box, or variable, by the name you give it when you make it. However, you want to be able to access this variable throughout your entire Flash game/movie, so you need to make it global. So in your first frame, which should be your start screen and isn't repeated when playing the game, you need to add some code. Make sure that you have a frame in your game that doesn't interfere with the game when playing. Since you have to assign a value to the variable, we will assign 0 to it when we first create it. However, if you initialize it in a game frame then during your game the score will repeatedly be made to 0 again. But you also want to have it at a frame that you visit when you restart the game, so your score is set to 0 again.
Anyways, click on the frame. With it selected press "F9" which opens the Actionscript editor. In this frame type the following code:
Also, if you haven't already done so, add the code "stop()" to the line underneath that one. So what does this code do? you ask. The "var" declares that you are making a variable and it should be global throughout the instance, in this case the main timeline. "score" is the variable's name, you could make this whatever you want but you need to remember it in order to access the variable. ":Number" says that this variable is a number. And finally "0" is the value that we give to the variable.
Now that you have your variable you want to display the score in your game. The way you do this is to make a textbox and show the score there. Click on the Text tool in Flash. Now, in the properties inspector, select "Dynamic Textbox" from the drop down menu. Then, in the "var" text field in the lower left hand corner, type "score", which is your variable's name. Customize the rest of the settings to your liking, for example the font and color, and when you are done click and drag to draw a textbox where you want the score to appear. Don't put any text in this textbox, but if you want to make a label add another box just in front of it that is a static textbox. You can click and drag this textbox with the pointer tool, as well as copy and paste it if you want more like it in other frames.
The bottom inspector should look like this:
Now you want to be able to change the score. To access your score value from anywhere but the main timeline type "_root.score". The "_root." tells the code to go to the very top level, the main timeline, and look there. "score" is your variable name. If you are adding this code in the timeline you just need "score". Now, in the Actionscript where you want to change the score, you would add something like "_root.score+=10". "_root.score" is your score variable. "+=" means that you want to add to the value you already have. "10" is just a number value that you should change to fit your game. You put this where you want to increase the score, for example, in a hitTest conditional. (Note: If you want to subtract from the score, just add a negative value or switch += with -=).
I hope that this works for you! If you have problems feel free to email me at email@example.com.
|» Level Basic|
Rating: 7 Votes: 15
|Luke programs in Flash, Java, and PHP to make games and websites in his spare time. His website, Zazeran.com, is an Internet Archive of games and tutorials. Please visit it and submit any games or tutorials you have!|
|Download the files used in this tutorial.|
|Download (0 kb)|
|More help? Search our boards for quick answers!|