New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

Flash Professional CS5: Creating a Simple Game for Android Devices
Illustration by Don Barnett

Adding lasers


From:

Flash Professional CS5: Creating a Simple Game for Android Devices

with Paul Trani

Video: Adding lasers

So let's go ahead and see where this file's at currently. I am just going to do a test movie. We have our intro. I'll click Play. And as I get into the game, you can see that I can move my character, but I really can't destroy any of these UFOs coming down. So I want to go ahead and add some lasers to my ship, and I want to have those lasers animate up. So those lasers are, of course, ultimately going to destroy these UFOs coming down. All right! So I am going to do that by using code snippets.
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

Adding lasers

So let's go ahead and see where this file's at currently. I am just going to do a test movie. We have our intro. I'll click Play. And as I get into the game, you can see that I can move my character, but I really can't destroy any of these UFOs coming down. So I want to go ahead and add some lasers to my ship, and I want to have those lasers animate up. So those lasers are, of course, ultimately going to destroy these UFOs coming down. All right! So I am going to do that by using code snippets.

So I do have this Mobile Game folder in my Code Snippets panel. And just reference the Code Snippets movie, if you don't have this. And I am going to add the Add Timer because that's what I want to do; I want to shoot this laser right over here at a certain interval. So I am going to add a timer. So I am just going to double-click on that. It adds that code to my Actions panel, right at the bottom actually. And let's take a look at this because there's an array that's created that's really going to hold all of the lasers.

Well, this first variable, I want to put it where all my other variables are. So I am going to cut it, and I am going to move it clear up to the top. Ok, right up here. This is where I want to put my Lasers:Array. So I am telling Flash that that lasers is an array and treat it accordingly. Right here, this Lasers equals new Array, well, that's actually where the array gets created. So I am going to cut that, including the comment, and I am going to move that into where the game is initialized. All right! So let's move this on up right in here, this initGame function right in here. We can see the player's created, enemies are created, and right in there I can just go ahead and paste in lasers, and I'll just hit the Tab key to move that in.

So amongst these other items being created, this Lasers: Array will be created as well. Now, let's take a look at the timer. So here's the timer. And since this timer is going to be dealing with these lasers that come from our player, well, I want to go ahead and take that and I want to move it up into place as well. So I am going to cut this, all the rest of that code, and I am going to move it up above the enemies, above that playGame, right in here. So you have moved player right in here. Well just below that, about line 113, and I'll paste that in.

Some now let's take a look at this code, because I do have this laserTimer timer that is created. So this is its specific name, and note that it's going to ahead and execute a function every half a second. And that's what this 500 is. It's actually milliseconds. So, say for instance, if I wanted it to fire every second, then that would be 1,000 milliseconds. But right now I'll just keep that as 500. So every half a second it's going to go ahead and add this timer, and it's going to fire off this function called the timerListener right here.

And this timerListener really does all of the work. It says, you know what? Let's go ahead and make a new movie clip called tempLaser from our Laser that's in our library. So let's just make sure it's in there. Move that off to the side. And sure enough, in my library you can see Laser is the linkage name of this item right here, capital L. That looks good. So, it creates it from the library, and it gives it an x and y position based on the player's position.

So I am going to go ahead and pin my code-- press that pushpin-- so I can select this player. OK, now let's review this, because basically it's saying, you know what, basically place the laser at the tip of the player's ship. All right! So it says it's going to place it at the player's x-position. If I just had that, it would put that laser right at this crosshair right here. I not only want it to get into that position, but I want to take in account the player's width, and I want to cut that in half.

And that will give me this point right in the center right here. So that's what this is. Take the players width, cut it in half, and make sure it gets added to this x-position. Ultimately it ends up at right there in the middle, which is perfect. Place its y-position, so vertically place it right on the tip right where that ship is as well. Give it a speed of 10, which we could always change that later, and then also go ahead and put it into this lasers array as well. So, just load up to that box full of lasers.

We'll need it later anyway. But do add it to the stage, and that's what addChild is. So that creates the timer. Well, in order to activate the timer, I need this laserTimer start. If I wanted to stop it, it would be, you guessed it, stop. I'll just undo that. So when do I want to start this laser? Well, I am going to cut this, and I am going to move it up into where the player is created. So right in here, about line 88 it says startPlayer and right in here addChild.

That's when the player gets added to the stage, so right after it gets added to the stage, start firing the laser. So immediately after it's added, start shooting that laser. All right! Great! Let's go ahead and run this. I'll do a test movie. Click Play. Now they are just going to get attached, so you can see them being attached right next to the ship in their correct position right at the tip. That's perfect, okay so far, for what I am doing. But now I need all of these lasers to travel upwards.

Okay, so as they get attached to the stage, I want them to move upwards. All right! So let's go ahead and see some of that action. And in order to do that, I am going to go to my Code Snippets panel and I am going to add Move Laser. So I am going to double-click on Move Laser. And it really is just a function. It does say that there is a Lasers array that is required, which I just added that earlier. And if there is a Lasers array, basically there is going to be this function, and it's going to go ahead and loop through that array, and for each laser in the array, which is known as i, it's going to go ahead and move it up.

So that's what this says right here. Okay. Move the laser up based on the speed defined earlier, the tempLaser speed. So we go ahead and move its y-position minus equals. So whereever it currently is, go ahead and start moving it up every time this function is being called. And then if the laser, say hits the top, then I can go ahead and add in a trace statement right in here, within quotes, laser just hits the top.

So I'll deal with sort of removing it later. But that's what I want to do. In fact, I am going to ahead and take this whole moveLaser function and I am going to move it back up into position, because I want it to be within the context of the code that it's dealing with, which is just beneath the laserTimer, so about line 130. Paste it in, and I would say that's look pretty darn good. The only thing I have left to do is call this function moveLaser. So I am just going to go ahead and select that function name, and I am going to copy it.

And I am going to move down right in here the playGame function. While I am making the enemies and moving the enemies, well, let's just go ahead and paste that in, move that laser as well, just like that. So as long as that we are in this playGame state, the laser is going to be firing, or actually moving in this case, as well as firing if you will with that timer. So let's go ahead and test it. Click Play. Sure enough, we can see them coming out of the tip, going upwards, traveling off the stage.

Everything looks pretty good. You could see laser hits the top and again it's sort of displaying that over and over again. So I would say that looks pretty darn good. The next step now is to really have more interaction between these lasers and the UFOs.

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

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

Notes cannot be added for locked videos.

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.