Start learning with our library of video tutorials taught by experts. Get started

Flash Professional CS5: Creating a Simple Game for Android Devices

Saving and loading game results


From:

Flash Professional CS5: Creating a Simple Game for Android Devices

with Paul Trani

Video: Saving and loading game results

This game currently does a good job of just keeping track of the level the person was on and their score when they finish the game basically, by the time all of their players die. In fact, they can even enter in their name. But none of this data is being stored, and what I want to do is I want to store the highest score, known as sort of the all-time high score. So that's what I want to store, and each time you play it's going to compare your score against the all-time high score. So that's what I am going to do.
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

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Flash Professional CS5: Creating a Simple Game for Android Devices
3h 35m Intermediate Feb 15, 2011

Viewers: in countries Watching now:

In Flash Professional CS5: Creating a Simple Game for Android Devices, author Paul Trani shows how to translate existing Flash skills from the web to mobile devices while designing a game in Flash and publishing it as an AIR for Android app. The finished application includes collision detection, random enemy creation and movement, shooting capabilities, multiple levels, and even a high score screen. This course also goes beyond game functionality and shows how to use mobile capabilities such as the accelerometer and gestures to control graphics, use the hardware keys to activate menus, and also how to optimize content so it plays well on mobile devices. Also included are instructions for distributing an app through the Android Market. Exercise files are included with the course.

Topics include:
  • Designing for mobile platforms
  • Creating and optimizing game graphics
  • Adding random movement and interactivity
  • Moving players based on the accelerometer
  • Using gestures
  • Detecting collisions
  • Implementing audio
  • Adding scoring and levels
  • Implementing a high score screen
  • Publishing an app on both Mac and Windows
  • Uploading to the Android Market
Subjects:
Developer Web Games Projects
Software:
Device Central Flash Professional Android
Author:
Paul Trani

Saving and loading game results

This game currently does a good job of just keeping track of the level the person was on and their score when they finish the game basically, by the time all of their players die. In fact, they can even enter in their name. But none of this data is being stored, and what I want to do is I want to store the highest score, known as sort of the all-time high score. So that's what I want to store, and each time you play it's going to compare your score against the all-time high score. So that's what I am going to do.

I am going to go into my File, this first frame here, and open up the Actions panel. And right down here, near the bottom, is this Show Results function, and there's a couple if else statements. But what I want to do is just after sort of turning off a couple this button and the high score name, I want to go ahead and basically create a shared object. It will store the all-time high score.

That's what I am going to create. So I am going to say var so, for SharedObject. SharedObject. Again, as I type that it's highlighted and I could hit Enter to select it. SharedObject = SharedObject.getLocal. So I am going to get the local shared object. This is going to be the local shared object on their phone. And then I need to give it a name, so I can say "alltimeHighScore" and that's the shared object that's going to be stored.

So I have created this shared object, and within this first if statement what I need to do is I need to go ahead and see if there is a current score in there. So for the shared object, s-o, for that shared object, the data property, score, for that score, which I'm just going to define, says for this shared objects data score property is it equal to anything? Or is it undefined. Because if it's undefined, that's when I want to go ahead and show them this message.

It says, "You made it to level whatever, and go ahead and enter in your name." So again, if this is undefined, so go ahead and display this message. Now the other reason I want to display this information is if they have the highest score. So again, two of those vertical bars, those pipes as they are known, and I am going to basically say if it's undefined or if the score is greater than the data in that shared object, known as score.

So if you basically got the highest score or if it's undefined, then go ahead and display this message saying congratulations, go ahead and enter in your name. So that's what this does. We want this to happen, and it will happen the first time through. So that's we are going to do. I am going to scroll down a little more. And else if the player didn't get the highest score, then display another message. It says your score of whatever doesn't beat 0. Well, your score of whatever you had does not beat--just outside of that string.

I am going to add a Plus sign--so.data.score. In fact, what I also want to do is I want to go ahead and add the person's name as well. So let me just scroll down here a little more. Your score of whatever doesn't beat this other score "by" so.data.name. And then we will even. I do have to put a Plus sign right in here, but down here at the end, we will go ahead and put a period at the end of that sentence.

So if your score of say 3 doesn't beat the high score of 5 by whoever. So basically that's what that says. So that looks pretty good, but I need to go ahead and save the score and even save the person's name if they happen to get the high score. So remember, we turn on the high score name and the Enter button. Let's take a look at that right down here. Again we are basically turning on and making that the enter button "work" if you will. So here it is.

Here is my Enter button. It does say, "Great job," yada yada yada. It drops in their name and all that good stuff. But what I want to do in here, because this is only displayed if they haven't entered in their name yet or if they have the highest score, so in here what I need to do is I need to define all of the elements within that shared object. So define all of the properties of the shared object, known as s-o.

So right in here I can go ahead in so.data.score = whatever score they had. I can even save the level that they were on, so.data.level = level and so.data.name. What about that name? I can't just type in name. What the name is is it actually has to equal to whatever they entered in within this highscore name.

So I need to actually take this and copy it and paste it right in here. So for that endScreen that highScoreName text field, well, whatever is in that text field needs to be saved in the name for that shared object. The last thing I need to do is I can go ahead and say, for that shared object, I am going to actually go ahead and flush it, which is basically push that data into that shared object.

That's all that does. Now if you do quit out of the game, it will automatically perform a flush, but you can always add that right in there, just to be on the safe side. All right, with all that working, I am going to go ahead and run this project, do a test movie. So the first thing I need to do is establish a score to begin with.

All right, you make it to level 1 with a high score of 8. Enter your name below. Paul. I will click Enter, Great job, Paul! You made it to level 1 with score of 8! This is inside of Invaders.swf, so if you could imagine this on my phone, the great thing is this also works on the desktop. So what I'm going to do is I am just going to close this file out and I am going to open it back up again. I am not going to republish it. I am just going to go ahead and go to my desktop. Here is the SWF that was just created, and again as if I was going back to this game and playing it again, that's essentially what I'm doing.

But again, it stores that data. Whether you're on the desktop or the phone, it works either way. So I am going to try to beat, say for instance, the score of 8. All right, you made it to level 2 with a high score of 14. Enter your name below. Paul number 2. Enter it.

So it works just as expected, just like before. Now when I play this game one more time, what's going to happen is I am not going to hit 14. I am going to get something lower so it's not going to display either one, this text field or that button. Okay, so I will close this down, go back out to the SWF. I am going to launch this SWF, and this time I am just going to die immediately. All right, there we are. As you can see, your score of 1 doesn't beat 14 by Paul 2.

So that was my second time through the game. So as you can tell, it is actually storing that data. It's storing that shared object data, also known as the Flash cookie, which is another slang term, and it works quite well. Now, there's other uses for this as well. You can not only store say the highest score, but you can also store the various levels. The person can come back and play that same game at the level they left off at, or what you could even do is you can extend it further by adding a SQLite database.

But for the purposes of this game, this is really all I needed to do.

There are currently no FAQs about Flash Professional CS5: Creating a Simple Game for Android Devices.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Flash Professional CS5: Creating a Simple Game for Android Devices.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

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