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

Adding chance and randomness

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

Video: Adding chance and randomness

Let's take a look at where the game is at now. I'm going to do that by just going to do a test movie. I'll click Play. Notice that really my spaceship doesn't stand a chance against all of these UFOs. There's absolutely no way. So what I want to do is I want to create an element of chance, so maybe only a couple come down. Maybe a little more. Maybe a little less. It all just kind of depends. Another thing I want to do is for the little spaceships that come down, I want them to come down kind of wobbly and a little more random than they do now, as opposed to just straight down.

Adding chance and randomness

Let's take a look at where the game is at now. I'm going to do that by just going to do a test movie. I'll click Play. Notice that really my spaceship doesn't stand a chance against all of these UFOs. There's absolutely no way. So what I want to do is I want to create an element of chance, so maybe only a couple come down. Maybe a little more. Maybe a little less. It all just kind of depends. Another thing I want to do is for the little spaceships that come down, I want them to come down kind of wobbly and a little more random than they do now, as opposed to just straight down.

Okay, so that's the goal, and what I'm going to do in order to do this is I'm going to go ahead and use this Mobile Game folder, these code snippets, and I'm going to select Random Chance is the one I'm going to use. Let's just double-click on that. It adds it to my Actions frame and right in here, notice that it establishes a variable called level, which is a number. All my variables need to go to at the top, so I'll just cut that out and scroll up to the top and paste that variable in there, because depending on the level will determine how many of those little spaceships appear.

Okay, so level 1, where would I put this? Well, I would want to put this where the game initializes. So, let's just go ahead and take a look at that. So, I'll cut that and scroll up right where I initialize the game. Here we are, initialize the game between lines 60 and 70. It's where I create my players. And sure enough, right in here-- in fact, I'll just put it right above those other items, level 1.

Okay, so we're initially going to set it to level 1, and it's going to create the player, and it's going to create the enemies. And when I create the enemies, I want to drop this in, okay. I'm going to drop in this code where my enemies are created, so I'll cut that, scroll on up, and right in here where I create my enemies, that's actually where I want to add a little bit of an element of chance, okay. So I'm just going to paste that in. Okay, so I'm going to create this new variable, and it's called Chance.

It is a number, and it's going to be a random number between zero and 60. That's my chance number. And for that chance number, if it's less than or equal to one plus the level, then go ahead and do something. In this case, what I want to happen, that do something happens to be create an Enemy. There we are. So again, open curly brace encapsulates creating an enemy and then that closing curly brace, closes it.

So, I'm going to click this Auto Format button. We can see how it nests everything together. All right, so again, right in here, pick a number between zero and 60, and then right in here, if the number is less than or equal to one plus the level, so that could be two on up, okay.

So it's the equivalent, at least in this first case, of typing in 2 right there. It may seem like a pretty small number, but keep in mind that this make enemies function gets fired off 24 times every second, okay. So sure, it might be a small chance that this happens, but that's going to mean less enemies that are generated, and also keep in mind that it's going to still fire off 24 times every second. Okay, so that's all I need to do.

Just if you meet these requirements, go ahead and create an enemy, so let's run this. Click Play. That's more like it. This is manageable; I can probably shoot them down a lot easier. But now that they are less of them, I noticed they're pretty straightforward. They're coming straight down, and really I want to make them wobble a little bit. So let's go ahead and add some additional functionality to them. For the move enemies, I want to add a little bit more randomness than what I have right in here, okay.

So let's go ahead and take a look at the Code Snippets panel, because I want to have some random rotation X and even Y movement. So I'm going to double-click on that. It adds that code down at the bottom, and I'm going to go ahead and move this code right into place. So I'll cut it and then move it right into move Enemies right here, so just paste it on in there, and then I'll click the Auto Format button.

All right, let's go ahead and take a look at this now, because first off, what happens is it's automatically going to rotate the enemy between 10 and 5 degrees. Technically those are just going to be the numbers. Okay, but it's just going to go ahead and rotate it, and what is it rotating? It's rotating the tempEnemy, so I'll copy that and paste it in. So go ahead and rotate it any random number between 10 and 5. Rotate it that direction. Make sure you round it, so we land on a whole number.

Next up, find the rotation and move the X position in that direction, okay. So right in here, notice this looks pretty complex, and let's face it, it does look complex. Let me just paste in tempEnemy, right in here, for these instance names, and it looks a little bit daunting. But know that all that's really going on here is this is a degree, so it's getting this degree, whatever angle it's at, and then it's converting that to radians. Okay, so Flash uses radians, so that's why I need to have math sign.

But this chunk of code right here, all it does is return the angle in which I want this enemy to travel, okay. So let's not get too hung up on the fact that there is some math in there. That's what it's going to do. It's going to start moving that direction based on the rotation, and not only that, it's going to move it that tempEnemy speed, which is right up here as well as within this area where we actually make the enemy, okay. So that's where that comes from.

We're going to go ahead and move its X position based on the rotation. Not only that, we're going to move its Y position based on that rotation as well, and I'll just paste in where it says instance name here for all of those. Okay, keep in mind that I still have this tempEnemy Y, and I want to go ahead and remove that line as well. So remove that. This is my new movement, and that's exactly what's going to take place. In fact, the last thing I need to do is just to make sure that it stays within bounds.

Okay, so if it's off the stage, move it back onto the stage, whether it's on the left or the right side, and this would be the right side. Just remove some of these empty spaces like that. I would say we're looking pretty good here. In fact, I'll just click the Auto Format button. It doesn't throw me any errors. So let's go ahead and test this out. Click Play, and there he is coming down.

There are all kind of wobbliness, exactly what I want. It kind of seems like they're attacking me. I don't want to have any higher of a number because they could start traveling upwards if that's the case. You can see some more are to kind of traveling like upwards, which is fine, but again, they're trying to attack. So that's why I have that number between 5 and 10; that is the random number. But this is manageable. It seems pretty fun. I can always adjust the levels if I want maybe less of these enemies to come down, but it looks pretty good.

You can see how adding this element of chance and the randomness can really create some excitement for your game.

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 preferencesfrom 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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.