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 lasers

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

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.

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.

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.