Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
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.
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.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.