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

Flash Professional CS5: Creating a Simple Game for Android Devices

Optimizing ActionScript


From:

Flash Professional CS5: Creating a Simple Game for Android Devices

with Paul Trani

Video: Optimizing ActionScript

Optimizing your ActionScript code is just as important as optimizing your graphics. That's what I want to do now. I am going to go into my ActionScript code. Here it is, and quite frankly, there's a lot going on. I need to optimize this, and what that means is I need to go ahead and look at ways that I can condense down some of this functionality, whether there's enter frames that can be condensed down maybe into one enter frame, or if there are for loops that can be condensed down as well.
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

Optimizing ActionScript

Optimizing your ActionScript code is just as important as optimizing your graphics. That's what I want to do now. I am going to go into my ActionScript code. Here it is, and quite frankly, there's a lot going on. I need to optimize this, and what that means is I need to go ahead and look at ways that I can condense down some of this functionality, whether there's enter frames that can be condensed down maybe into one enter frame, or if there are for loops that can be condensed down as well.

I also need to take a look at expensive processes, and when I say expensive processes, I mean, more than anything, enter frame events. Okay. So I am going to go ahead and take a look at my enter frame events, and I want to make sure there are not a lot of them, for one, okay. So as I scroll down, about line 62 there's this Enter_Frame game loop, and this constantly checks the game state we are in. And that's fine, but what I am going to do is I am going to select this word and I am going to do a Command+F, or Ctrl+F if you're on a PC, and I am just going to do a Find Next.

All right, here is another one. Here is the enter frame for moving the player. Well, I need to do that as well, so that one is okay to keep in as well. Find Next, and again, this is a remove event listener. I, in general, only have two enter frame events: one for the game loop and then one to move the player. So that looks pretty good. But I want to go beyond an enter frame event, just looking at how many there are, because I need to be aware of how often this enter frame event is firing off, and it does so at the frame rate of the movie.

So I am going to pin my code, and I am going to take a look over here in my Properties panel. The Frames Per Second for this document is 24. I might consider dropping that down to, say for instance, 20 frames per second. Okay, it should still play smooth and look pretty good while not being that taxing on the mobile device. If I drop it down to say 15, it's going to get a little more choppy, so I think 20 is pretty good for this project. Okay, so the enter frame looks good. I am going to go ahead and just take a quick glance at some of the other code as I scroll down.

Let's go ahead and take a look at, let's say for instance this timer event. This works out pretty well, and these are really good to use because I get to determine, independently for each timer that is, how often it executes, and that one executes every half a second, so that's pretty good. I might want to take a look at maybe moving my enemies or even, like, testing the collisions for instance. Let's take a look at these testing collisions. 284, that's where the function is, and I am noticing that this loops through the enemies array.

Okay, it loops through the enemies array, because it checks to see when an enemy and a laser collide. So that's what this test for collisions does, okay. But I might consider combining this with something else. In fact, if I scroll up, I can see that with this moveEnemies there is this same for loop. So I want to go ahead and combine these two. In fact, I want to get rid of this test collisions altogether. All right, so in order to combine this, first thing I am going to do, since I am not going to need this anymore, I am going to remove that enemy for loop.

Anytime you remove an opening curly brace, you need to remove the closing curly brace as well. Same thing for this function as a whole. I can remove it, remove that closing curly brace, and I don't need this tempEnemy anymore either, because it's going to be up above. There we are. So this is what I have it condensed down to. So this is a lot better. I have eliminated that for loop. I can go ahead and take this code, and in fact, I did notice one more thing right down here for this break enemy.

Well, that doesn't exist anymore. It's not in that for loop, so I can remove that as well. So again, I am going to take this chunk of code and I am going to cut it, and right up here within this for loop, right in here, after this closing curly brace right here, hit Enter, and this is for the Make Explosion, and paste that in. Lastly, I will go ahead and click my Auto Format button, just to make sure everything nests in nice and neat, and I'd say that looks pretty good.

But keep in mind that I did go ahead and I removed that test for collision basically. So right up here in this playGame, since that testCollisions function no longer exist, I need to remove it as well. And anytime you're removing a function, it's always good just to select it and again just find it, so Command+F or Ctrl+F and just click Find Next, just to make sure it's not anywhere else, if you're removing things. So this Find and Replace is going to be really helpful when you start condensing down your code.

All right, so that's fine. I can remove that, and again, I am just simplifying this code. And again, I can go in and kind of clean it up and make sure everything is pretty squared away. A lot is going on here, and I'd say this is pretty powerful. Know that it actually does not execute any of these commented lines, so I don't need to worry about that. Trace statements, I am not going to worry about those either. In general, I think this game is good to go. I will do a quick test movie in Flash, just to make sure everything plays correctly.

(explosions) All right, so everything was playing well. It seemed pretty smooth. I liked how it was working, but the next step is to really try this out on a device. Now, you are not going to get ideal performance compared to a Desktop, but again, you want to make sure you optimize all your graphics and your ActionScript code before you release it or publish it into the Android marketplace.

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