Bubble shooter game tutorial for adding score
This is a lesson that's taken three months to learn, so I decided to share it. (This also makes it available to me for future reference, too!) (With a little help from a friend to get things started *Sukhendu* you're wonderful)
In the book Flash Game Development by Example chapter 8, a game called Puzzle Bobble is created, but there's no score keeping methods or anything fancy. Matter of fact, the end of the chapter suggests making it a multiplayer game. Well, I can honestly say the chapter's easy enough to follow with some guidance from the source files. Somehow I managed to misspell things here and there and had a few other little issues with the game, but by itself, it didn't take long to make.
The concept of the game is simple. As the beginning of the chapter explains it, it's "a "match three and remove" tile-based game." All you do is use a fixed position cannon to shoot a randomly colored bubble. Match three and the bubbles are removed. Nice and simple. Well, try finding a tutorial for it online - that's next to impossible. I never did succeed in finding it in any place other than the book.
Now for the part the book doesn't cover: score.
Hopefully, you have a light knowledge of Actionscript 3, but if not, I'll try to explain the best I can, but bear in mind, I'm still learning too! After following the book's tutorial, you'll have a functional game (right? Right.) I'll number off the lines so it's easy to put the new score code in the right place.
5. import flash.text.*;
22. private var gameScoreField:TextFIeld;
23. private static const pointsForConnection:int = 100; // that number can be anything you want as long as it's not 0 or nothing will happen when you make a match.
24. private var gameScore:int;
227. gameScore += pointsForConnection;
228. showGameScore();
244. public function showGameScore() {
245. gameScoreField.background = true; // this creates a background behind the text field
246. gameScoreField.backgroundColor = 0x00FFFF; // this can be any web color you want, as long as you know the color's code
247. gameScoreField.x = 545; // this places the text field over on the right side of the screen where it doesn't overlap the game.
248. gameScoreField.text = "Score: "+String(gameScore);
249. }
Things like "gameScoreField" and "pointsForConnection" can change, but this is a nice simple means of adding score to that game. This can also be applied (in theory, I haven't tested yet) to other games.
I'm really not very good with the terminology, but after following the tutorial, I'll start with line 5 and try to walk you through the code. The book and tutorial really explain the import flash stuff better than I ever could, but in this case, we're telling flash to import all the properties for creating text. On line 22, we're telling flash to create a text field with the variable gameScoreField - the capitalization isn't really super important, but it helps for identifying the variable later, sort of a programmer's preference recommended by just about every book I've read on actionscript. Line 23 creates a constant called pointsForConnection in which we tell it to create an integer of 100. That can be any whole number greater than 0 in this case. On line 24, we're creating the variable for gameScore that we call later within the code for the removal of a matched connection of bubbles. On line 227 we call the gameScore variable and tell it we want to create the points for each connection, referring back to the static constant pointsForConnection which was 100. Then we tell it showGameScore, which will tell our later code to display the score as it's created, updating in real time as a match is made. On 244, we're creating the showGameScore function in which we finally create the gameScoreField text field to display the score, the background and position using x coordinates of the score field. Finally gameScoreField is told to display on 248.
Even though this was really meant as a reminder for me, I do agree that a more thorough explanation was called for and a link to the book could be handy.
Comments
Good idea, thank you. I really wrote it on the spur of the moment just to make sure I didn't forget the code or anything like that.
Wonderful ideas and insightful questions, but I'm still learning AS3 myself. The idea of the tutorial was to follow the book's instructions for making the game, where the author explains far better than I ever could the mechanics of the game, and then use the code I provided on the appropriate line numbers. What I posted in this little tutorial is generic score keeping code. It could be used with any game, I merely used the bubble shooter as an example (mostly to remind myself rather than writing it down on paper and having it get lost somewhere). I'll probably add more to this tutorial - especially on text field properties - as I learn more. One good idea would be to look up an AS3 cheat sheet - those tend to have the text field properties listed somewhere and might provide an answer to your questions relating to that.
This hub is probably going to continue changing for quite a while, but its simplicity (combined with that book, which I did put an amazon link in for finally) would be enough to create a simple score display setup.

dannystaple 4 months ago
I'd love to see more explanation of the bits added and how they fit in, than just line numbers and diffs. I know more AS2 (and many other languages) than AS3, but this is all fairly simple.
It'd be great to pop a screenshot of the added score.
You may be able to use some formatting/style to give the code the right look - with a monospace font, and coloured text for variable identifiers and things.
You could break down the changes - adding a score keeper, then rendering the score. I also wander is show game score should be in the general game loop (not having seen the rest of the code), and does the textfield need to have all it's attributes reset when just updating the value? I've also not seen the y value of the textField, or size and font attributes mentioned - are they automatic or did you place them graphically?
You are definitely missing a trick by not having an amazon link to the book itself - this would definitely enhance the page, and give you potential monetization.