Video: Adding explosionsLet's go ahead and take a look at where this game currently is in its development stage. I'm going to click Play, and if I move this SWF over a little bit, notice that there is a couple of collisions going on here, these lasers with these UFOs, and we should see these trace statements appearing. So I need to actually have an explosion when these lasers hit the UFOs and when these UFOs hit the bottom or hit my player. So I need to attach this explosion from the library in a particular position.
- Next steps
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.
- 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
Let's go ahead and take a look at where this game currently is in its development stage. I'm going to click Play, and if I move this SWF over a little bit, notice that there is a couple of collisions going on here, these lasers with these UFOs, and we should see these trace statements appearing. So I need to actually have an explosion when these lasers hit the UFOs and when these UFOs hit the bottom or hit my player. So I need to attach this explosion from the library in a particular position.
So let's go ahead and go to the Library panel, because in the Library is going to be this explosion movie clip. I'll just double-click on it. Notice it contains a couple of frames of this explosion. So this is what I want to attach to the stage and then play, and then this last frame is just a stop action so it doesn't continually loop, but that's basically what my explosion movie clip is. I am going to go back to Scene 1, and in this first frame of this Actions layer is where I want to add a code snippet.
So I am going to open up my Code Snippets panel. In the Mobile Game folder, right down here is Make Explosion. Please refer to the Code Snippets movie if you don't have this. But all I am going to do is just double-click on Make Explosion, and it adds this code here. So what happens is it creates an array called explosions. So here's the explosions array, and then there's that function that's going to deal with this array. But the first thing I'm going to do is I'm going to go ahead and take this variable and I'm going to cut it, and I'm going to move it up to the top of my Actions panel. Right in here, just beneath Lasers, I am going to go ahead and paste in this variable explosions.
So now Flash knows that explosions is an array. Next up, I want to go ahead and create the array, and that's what this line does. I am going to take that in the comment. I am going to cut it, and I want to create the array when the game starts. So I'm going to scroll up toward the top, and right in here, when the game initializes, these other items are created, and sure enough, just beneath Lasers, that's where I want to paste in my explosions array.
So I will hit Tab just to even those out. So the explosions array is created. Now, let's scroll back down, and let's take this. So this Make Explosion, I'm actually going to take this and move this up as well. I am going to cut it, and I am going to move it up just beneath my testCollisions, about line 237 and I'll paste it on in, and let's take a closer look at this. It says add an instance of explosion from the library to the stage, and it's putting it at 400 pixels in by 300 pixels down.
That's the X and Y. So let's go ahead and make sure that there is an explosion in my Library. Sure enough, its linkage name is Explosion, capital E. So here is my makeExplosion function call, and it's going to go ahead and pass in these two numbers, which is just the position of this explosion. But this makeExplosion actually creates this tempExplosion. That's the name of this new movie clip. So it basically grabs this Explosion symbol, puts it on the stage-- that's what this does--and it puts it on the stage, its x position being ex, which is right here.
So it's pretty much whatever property that gets passed in. If this is 400, well ex actually equals 400. So it's the equivalent of having the position be 400 right there. I'll just undo that. Same thing for the y; whatever number gets passed in to this function right here again becomes the y position, and then it adds it to the stage depending on those two positions, and then it puts it inside of this Explosions array. So that's what it does. It pulls an item from the Library, places it on the stage at this particular location is how this works.
All right, I'm going to go ahead and just remove this comment, and quite frankly, I don't want to just call this function as it is right here. What I want to do is I want to determine when the--say for instance the laser hits the enemy, then make that explosion. Sure enough, that's right here. So I'm going to go ahead and take this line and I'm going to cut it, and right in here where the laser hits the enemy, well, I'm going to go ahead and replace that trace statement with this makeExplosion.
That's all well and good, and that will work when it gets hit, but I need to be concerned about the position now because I want to position it at the same location as the tempEnemy. So I am going to copy that word and paste it right in here where it did say 400. Remember, that's the x position, so the tempEnemy's x position, and the second parameter right here is going to be the y position. So make an explosion exactly where that tempEnemy is and attach it to the stage and actually put in the array.
All right, that looks good, and again that's just when the laser hits the enemy. Well, I'm going to copy this line because I also need to determine when the enemy hits the bottom or when the enemy hits the player, I want that enemy to blow up. So that's right here, trace("enemy scores"). Well, I am going to go ahead and replace that with that Make Explosion now as well. All right! So we should see all of these various explosions appear on the screen.
Notice that nothing is being removed from the stage or anything like that just yet, but we should see lots of explosions, which is always cool. Let's click Play, and again, as I come down, I'll go ahead and shoot this guy. You can see all of those explosions as I hit him, and then watch him. As he hits the bottom, it'll go ahead, and explode or has they hit my player, you can see all of these explosions all over the place. So that's great. It's all working very nice. The only issue is is that these UFOs are invincible, and the next step will be to actually remove items from the stage.
There are currently no FAQs about Flash Professional CS5: Creating a Simple Game for Android Devices.