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.
Movement in animation and games is often actually done with ActionScript because ActionScript allows for flexibility over the Timeline alone. I'm going to show you exactly what I'm talking about, because what I want to do is I want to animate this intro screen out. In fact, I want it to move over to the left-hand side when you click on the Start button. I also want to do the same thing for this end screen. So again, I want to create some animation using ActionScript, and I want to animate both of these things here.
So let's just go ahead and undo that move. All right. And again, I'm going to animate the intro screen, and I'm going to start off by using some code snippets. So in the previous movie, I talked about how to add to this Mobile Game folder, and I'm going to select Animate with ActionScript. And when I double-click on it, it will add that code to my Actions panel on that first frame right there, so I'll just double-click on it and it adds that code.
Okay, so the first thing I have here are the import statements, okay, so I need these import statements in order for this code to work down here, because this code down here references these various classes, and that's what these are, classes and packages. So you can see TweenEvent. Well, I'm using that tween event right here. So I need these three lines, so I will just take them three and I'll cut them and I'll scroll up to the top, because I want to make sure those import statements are at the top and the code is there before I start to actually use it, because this Flash ActionScript code executes from the top down.
So lets scroll down, and again down here at the bottom, I want to take this code and I want to cut it as well and put it in the appropriate area. And I'm going to scroll up. About line 18 you'll see this introScreen. When you click on that Play button, it fires off as clickAway, which then in turn fires off this moveScreenOff function, which is right here. So what happens right now is it just moves the screen's X position. It makes it equal to the screen's width, and it multiplies it minus one.
So what this does is it basically figures out the screen width, takes the screen width and makes it a negative number. So that would mean if it's normally say 800 pixels wide that number in there will be minus, or a negative, 800. That's what this does okay. So that's the current functionality, and this is pretty fancy. I actually want to use that in a second, okay. But again, below that right in here, I'll just paste in that code snippet that I added earlier, and I will scroll down a little bit.
Again, right in here for this intro tween I have this introTween variable, and it creates this new tween. And the first thing you needed to do is define what will be animated, and in this case I wanted to be the introScreen. So the introScreen is actually a variable that gets passed in, and inside of this moveScreenOff, it's actually known as screen, so whatever gets passed into this function it references back to that name. So all I really need right in here is not introScreen.
I just need screen okay, because I'm going to use this movieScreenOff function a couple of times. So again screen refers to introScreen. It's going to move its X position. That could be Y position. It could be its size. It could be its alpha or its transparency, if you will. But I want to move it to the left and then right. Okay, so that's the property I'm going to move. This is how I'm going to move it, so it's going to move it sort of in a strong motion from the right to the left.
Okay, there's bounce. There is also elastic that I can use. Next step, right here this is its starting position, its ending position, and then numbers in seconds that that transition will take place. So that's how the tween class works, okay. And for it's start position, well, I don't want it to start at zero, because, quite frankly, the screen isn't at zero. I want it to start from wherever the screen is, so I can type in screen.x, so wherever it is, start there and then move it to, not to a thousand, because that's going to move it to the right, what I want to do is I want to take this right here.
I want to take what is the equivalent of minus 800, or whatever the size of that item is, and that's where I want the ending position to these. So I'll cut that and paste that right in there. In fact, I don't even need this line right there anymore. Okay, so its starting position is wherever it is. Its ending position is its width in a negative number. So that's what this does. It's going to happen over the course of one second. True means you're setting the interval to seconds okay, and what this means is it doesn't matter what the frames per second of the Timeline is.
It doesn't matter. It's going to play and get it from point A to point B over the course of one second, regardless of the frames per second. But again, what I want to do--and I'm just going to pin this code, so that code doesn't moves because I want to keep it up--but I do want to change the frames per second to 24, okay. So that's what I want to happen. Everything on the timeline is going to play 24 frames per second. This is just going to get there in one second. And after that's done, when that motion has finished, then go ahead and fire off this tweenFinish.
Okay, so for the introTween right here, on motion_finish go ahead ahead fire off this function. Well, what do I want to happen during that time? Let me scroll down, because that's when I want to initialize the game, throw in a trace statement and add that intro frame. So I'll cut all of that code and move that right into that function right there. All right, so far so good. In fact, the last thing I'm going to do is I'm going hit this Auto Format button, just so it formats and indents all my code. I can see that this function is nice and neat now, and I'd say this is ready to go.
Okay, so again I've cleaned up this moveScreenOff function. So whether it's the introScreen or something else, it's going to animates it using this Tween class. But I'm using this moveScreenOff in another place. In fact, clear down at the bottom, right here, line 132, I'm calling that same moveScreenOff. And in this case it's going to move off the endScreen. And that's the power of ActionScript animation is here I'm able to also move the endScreen as well as the introScreen, all from within one function using the Tween class.
All right, so let just do a test movie. There it is. We see that Timeline animation. I'm going to click Play. We'll see it slide off to the left, and again it goes through the game, and then we have this high score screen. If I click Play Again, it's going to animate this off the screen as well, just like that. So two animations done pretty easily by using the Tween class as well as the on- motion finish. It really gives me a nice effect, and it also makes it reusable.
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.