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