New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Adding scoring and levels

From: Flash Professional CS5: Creating a Simple Game for Android Devices

Video: Adding scoring and levels

This game is coming along nicely, but what it's really missing is it's missing scoring and various levels. So if I click Play, as I destroy these UFOs, the score, nor the level will add up. There will be nothing in here. So my goal is to go ahead and populate those two text fields. So let's take a look at them. I am going to turn off these three layers: the introScreen, options menu, and the endScreen.

Adding scoring and levels

This game is coming along nicely, but what it's really missing is it's missing scoring and various levels. So if I click Play, as I destroy these UFOs, the score, nor the level will add up. There will be nothing in here. So my goal is to go ahead and populate those two text fields. So let's take a look at them. I am going to turn off these three layers: the introScreen, options menu, and the endScreen.

There is right in here for this text layer, if I just select the Zoom tool, I am just going to zoom in on that text here, just use my Selection tool to select these text fields. Let's take a look at them. If I select this score text field, I'll go to the Properties panel. Now notice that this text field, its instance name is score_txt. It's set to Classic Text and it is a Dynamic Text field. So I want to dynamically update the score as I sort of destroy those various enemies.

Also notice that the Font Family is just _sans. So it's going to use the default sans serif fonts regardless of the device. Not only that, the size is about 24 point, which seems large normally, but it's going to look great on the phone. Same thing for my Level text field. If I select it, its instance name is level_txt, it's a Classic Text field set to Dynamic Text, and has the same Character properties as well. So I am going to start basically with the score right here.

So I am going select it. The score_txt is what I am going to keep in mind, and I am going to select that first frame in the Actions layer and open up the Actions panel. So at the top what I have is I have this variable called level currently, and it is a number, because I am already actually using that and I'll show where that is. But what I want to do is I want to add another variable called score, and it's also going to be a number, just like that. All right, let's see where level is being used.

So I'll just scroll down, because when the game gets initialized, the level starts out equal to 1. Well, what I want to do with that variable is I want to put that number actually in the text field. So level_txt, its Text Property is going to be equal to the level number. I wrapped that in String because what it does is it takes this, which is normally known as a number, and it converts it to a string, so it just treats it as text, since it's going in a text field.

Again, I want to work on the scoring, so score is going to be equal to 0. So start out at zero, and then for the Score text field, its instance name is score_txt. Its text property, same thing. All right, so they both start out when it initializes, Level is going to be 1, the Score is going to be 0. And for the score, each time the laser hits one of the enemies, I want to add to the score. So let's go ahead and take a look at that. As I scroll down, for moveEnemies, I can see them move test for collisions. Right down in here for test for collisions I have is this.

So when the laser hits the enemy, not only does it make an explosion, does the enemy get removed, the laser gets removed, but right in here, add to the score. That's what I want to do. And I do that just by typing in score++, and what that means is is it takes whatever the score is and it just adds one to it. So that's just adding one to the score. The next thing I need to do is for the score_txt, for that text field, go ahead and update it with that score variable.

So it adds to the score and then it gets put in the appropriate place. So the score is all squared away. What about the levels? Where is that being used? Well, let's take a look. Let's scroll up, because when the enemies are created, this is where the level is being used. So remember, this is initially set to 1, and I will just undo that. But know that each time basically the enemies are being made, there is a chance that maybe, what, two enemies will be made, each time this function is called, so that's how that works, and that's just for the first level, there's maybe only two enemies that are going to come down.

But as the levels go on, I actually want more enemies to be created, and that's why if I undo that, it's going to add say level at--let's say it's going to had 2 to it level two, so there is a chance that three enemies will come down as well. So that's the number of enemies as the game progresses, as you advance in levels. Well, how do you advance the levels? So I will just undo that because what I want to do is I want to go ahead and right down here in the check for end, testForEnd, right here in line 276, testForEnd, right in here, this is what I want to do is want to go ahead and advance the level if they reach a certain score.

So right in here, I am going to go ahead and type in if the score, if it's greater than 10 for instance, then I can go ahead and advance the level. So if the score is greater than 10, advance the level. So that will be equal to 2 the first go round, and then also update the text field whichever level you are at. Okay, so that's if the score is greater than 10, then advance the level. But I always want this to be greater and greater as well.

So the first go around for level one, you only have to kill 10 guys. For level two, you have to kill 20 guys. So what I need to do right in here is just--I'm going to type in level*10. Multiply it by 10. Okay, so this makes each level a little bit harder, and it's going to be 10 the first level, 20 the second level, 30 the third level, and also more of the enemies come down as well, and of course that's updated in the text field. So that's how we deal with multiple levels.

It gets a little bit more advanced because more of the enemies are generated, and also I'm also adding to the score. So let's go ahead and test this. I'll click Play, and let's watch the score and the levels.

So I've just right-clicked on this SWF file, just to pause it. So you can see, I have a score of 23. I am now actually up to level 3, which is great. That's exactly what I want. So the thing I need to do next is I actually need to give my guy a number of lives, and they get subtracted as these UFOs reach at the bottom, because as it stands right now, I can just play all day racking up my score with no consequences.

Show transcript

This video is part of

Expand all | Collapse all
  1. 1m 48s
    1. Welcome
      1m 0s
    2. Using the exercise files
      48s
  2. 7m 16s
    1. Understanding the user
      4m 2s
    2. Flash content on Android devices
      3m 14s
  3. 17m 28s
    1. Reviewing the game
      1m 50s
    2. Creating a file in Device Central
      5m 39s
    3. Reviewing the game structure
      8m 23s
    4. Adding code snippets
      1m 36s
  4. 36m 13s
    1. Animating the intro screen
      8m 13s
    2. Moving the player
      8m 13s
    3. Adding enemies
      6m 49s
    4. Adding movement
      4m 16s
    5. Adding chance and randomness
      8m 42s
  5. 34m 30s
    1. Adding lasers
      8m 14s
    2. Detecting collisions
      7m 57s
    3. Adding explosions
      6m 26s
    4. Removing assets from the stage
      8m 50s
    5. Adding audio
      3m 3s
  6. 33m 12s
    1. Adding scoring and levels
      7m 41s
    2. Subtracting lives and ending the game
      4m 26s
    3. Creating a results screen
      5m 8s
    4. Displaying the score
      6m 57s
    5. Saving and loading game results
      9m 0s
  7. 17m 14s
    1. Detecting movement with the Accelerometer
      6m 39s
    2. Using the swipe gesture
      4m 42s
    3. Using hardware keys
      5m 53s
  8. 23m 18s
    1. Handling exits and idle mode
      3m 31s
    2. Handling activation and deactivation
      6m 14s
    3. Optimizing graphics
      7m 14s
    4. Optimizing ActionScript
      6m 19s
  9. 34m 41s
    1. Creating the application files
      8m 23s
    2. Creating the app (Mac)
      5m 5s
    3. Creating the app (PC)
      7m 19s
    4. Publishing to an Android device (Mac)
      7m 7s
    5. Publishing to an Android device (PC)
      6m 47s
  10. 8m 56s
    1. Uploading to the Android market
      7m 7s
    2. Downloading from the Android market
      1m 49s
  11. 1m 22s
    1. Next steps
      1m 22s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.