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 movement

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

Video: Adding movement

So let's take a look to see where this project is at currently. I want to go ahead and test this file. We have our intro, and when I click Play, you'll see lots of alien ships start to gather at the top of the screen, which does look intimidating. But at this point, you know what, I want them to basically attack, so I want these ships to start moving downward. And I'm going to do that by adding a code snippet.

Adding movement

So let's take a look to see where this project is at currently. I want to go ahead and test this file. We have our intro, and when I click Play, you'll see lots of alien ships start to gather at the top of the screen, which does look intimidating. But at this point, you know what, I want them to basically attack, so I want these ships to start moving downward. And I'm going to do that by adding a code snippet.

So in the Code Snippets panel, right down in here, Move Enemies. Again, I want to move many different things. I'm just kind of go ahead and double-click on move enemies, and it adds this code right here. Now that is plural, so it's going to move a lot of things, but let's take a look at this specifics, okay. Now, this is a complete sort of code chunk, so you can use this in many different projects, because quite frankly, there is this reference to this array. Well, I already have an enemies array, but basically you need to make sure you have an enemies array if you don't already have one. You need to create it in this case. I've already created that. Did that in the previous movie.

Also, you want to create a tempEnemy from the Enemy library as well. So right over here, there is my enemy, and that's been done already as well, so I'll just remove it. So it's basically two references to it just tell you not to forget to do that, because that's what's being referenced right in here. So for that enemies array, for however many are in that box, if you will, go ahead and loop through this for loop that number of times. This tempEnemy is going to be equal to the enemies and whatever time it's through, so it's going to loop through this whole box each time.

TempEnemy just refers to each and every item that comes through, but right here, this is where most of the action happens. It says "Move the tempEnemy down," so that'd be its Y position "from its current position" and then add to it plus, basically, its speed. Where does this speed come from? Well, let's a take a look. As I scroll up, right up in here, here is this tempEnemy.speed.

And that's what I want to do. I want to a reference this speed number, which happens to be 3. This will change sort of throughout the game, but I do want to move up this function now, okay. So I am going to take this moveEnemies function and the various comments, and I am going to cut it, and I'll scroll up. Right underneath makeEnemies, moveEnemies. This is going to loop through the contents of that enemies array and start moving all of those items down, based on the speed.

Okay, well how often is this being called? Well, let's take a look. For one, before I just wrap this up, I do want to make sure that this has :void, okay. So I don't want it to return anything or anything like that. It's just a common practice to add void there. And right up here makeEnemies, once they're made, I want to go ahead and moveEnemies like that. Okay, so that's when it's being called is right in here within playGame, which gets called plenty of times, okay.

So let's go ahead to see this in action. This is actually pretty exciting. So let's go to Control > Test Movie. Let's test it out. If I click Play, well, you should see all of those enemies start to march down, and here they come. Quite intimidating. In fact, there is not much I can do, especially since I can't fire. So that works pretty darn good. I really like how all of those enemies kind of come down. But really my next step with this is I want these enemies to sort of move at, sort of a random pace and kind of move from the left to the right and even tilt a little bit as if they're actually sort of coming through the atmosphere.

So that's the next step, but I'd say this is a great place to start, and now I can add individual movement to each one of these as well.

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.