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

Adding enemies

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

Video: Adding enemies

The next step in this game is what I want to do is I want to add multiple enemies to the stage. This enemy movie clip happens to be this flying saucer right here. So I want to add it to the stage many times, okay. So again, that's what I want to add, and I am going to add them using the Code Snippets panel. And again, I've added this Mobile Game folder earlier. That's in the code snippets movie is when I added it. And in order to add a code snippets, I'll just double-click on it. Create many enemies is what I want to do which normally, fundamentally I am against, but in this case, I think it's okay.

Adding enemies

The next step in this game is what I want to do is I want to add multiple enemies to the stage. This enemy movie clip happens to be this flying saucer right here. So I want to add it to the stage many times, okay. So again, that's what I want to add, and I am going to add them using the Code Snippets panel. And again, I've added this Mobile Game folder earlier. That's in the code snippets movie is when I added it. And in order to add a code snippets, I'll just double-click on it. Create many enemies is what I want to do which normally, fundamentally I am against, but in this case, I think it's okay.

All right, so the first thing that I am going to do is create an enemies array okay. So an array is basically a big box that can hold many things. That's what an array is. Unlike a variable that just holds one thing, an array can hold plenty of things. That's why it's called enemies. Okay, well what do we do with those variables? Well, we take them and I'll cut this variable, and all I need to do is just move it up near the top, okay, because I want to make sure all of my variables are together.

So I'm telling Flash, "Hey, you know what, enemies is an Array. Treat it as such." Scroll back down. Well, where do we create this array? Well, I wanted to happen when the game initializes. So I'll cut those two lines, including the comment, and I want to find out where the game initializes. Scroll up. There we are. Starting at around line 67, this is our initGame where the players created. Just below that is where I'll paste it, and here is where the enemies array is created.

So that array is created, and then I want to go ahead and start adding all of those enemies to that array when the game starts to play. Okay, so right down in here, this makeEnemies is what I want to get rid of because, again, I've added that function down at the bottom. So right down here, I want to take this function, I want to cut it scroll up. This is basically a skeleton of function. Nothing's really in it, other than that trace statement, and I'll paste it right in there.

Now surprisingly, it's actually got to go okay, but let's just kind of review it. Again, when the playGame function is being called makeEnemies, that function is being called and all of these various tempEnemies are created, okay. So I'm creating this variable called tempEnemy. It's a MovieClip. And what it does is it grabs the Enemy from the library. So right over here if I look in my library, Linkage, capital E for Enemy, grabs that movie clip, and it creates this tempEnemy from it.

And what it does is it gives it a speed = 3, because I actually might want to control this later on. Maybe as the levels go on, as you get further in the game, maybe the enemies get faster. The tempEnemies x position is = 400 and then go ahead and add that enemy to the stage okay. Not only that, but go ahead and push that enemy into that array as well. So take all those enemies, as you add them to the stage, you know what, put them inside of that big box known as the enemies array.

So that's what's going on. Okay, now I can run this. In fact, what I can do is I can go ahead and add a trace statement, and right in here, I can just go ahead and put in, in quotes tempEnemy. So we'll just see this print out, and we will see one enemy appear, but they're all going to start being stacked one on top of the next at the X position = 400. So let's just see what this looks like. As I test this out, it's not going to be anything too exciting, because as I hit Play, so I didn't change the Y position, but it starts stacking them up right here, okay.

And you could see right in here it says tempEnemy many different times. Well, it's created all of those various enemies. It's just it's puts them all in one position. So let's control that a little more, okay? Because I'd like to do is I would like to say, for instance, move those enemies so they appear at a random point along the X axis, so from the left to the right. So instead of 400, let's go ahead and enter in Math.random. As I type in rand, I can see that selected ,I can hit Enter, have the closing parentheses, and what that will do is that will get a random number between zero and one.

Okay, so that's what Math.random does. Okay, so I'll just copy this and add that as a note, a random from 0 to 1, which isn't a very big number, okay. So it's going to be on the 0 pixel or the 1 pixel. So what I do is I want to multiply that by, say for instance, 800. All right, now what's going to happen is it's going to get a random number between 0 and 800 is what that will do.

That great, because it's going to spread them all out, okay, but this random number actually gets anything from 0.0 to 1.0. So you could end up with something like 0.235, and those numbers I don't really want. I want to round them, so Math.round. I want to round the number either up or down depending on whichever is closest. Okay, so I want these tempEnemies to appear on whole pixels.

So it's going to get a random number, and in this case it just rounds it to the highest whole number is what that does. All right, this is more like it. I think this is going to work out pretty well. Again, it's still going to put it across the X axis. The Y position isn't going to change, and that's fine. Let's test this out, and let's just see what we're up against. Click Play. You could see them all being added right up there. So it gets kind of intimidating for our player as we see all these spaceships stack up. But obviously you can see that it's working, and again, the next step would be to actually start moving all of these various enemies down the screen.

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
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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.


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.

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 preferences from the dropdown menu.

Continue to classic layout Stay on new layout
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
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.