Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- 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
Skill Level Intermediate
One thing you are going to want to do with your game is you are going to want to optimize all of the graphics so it plays well. This is particularly important on mobile devices, because, again, they just have smaller processors. They are running on about a 1-GHz processor, and in general, I need to make sure these graphics perform well. What's interesting is that on mobile devices, vector graphics don't perform as well because these small phones have to do all these calculations of all of these various curves for vector drawings.
So what I want to do is I want to take my important vector graphics and convert them into a bitmap. Flash actually allows me to do that. For instance, if I just come to this text layer right here and I'll just zoom in, for instance, on some of this text here, well, this is actually quite frankly, this is some text here that is vector information. Okay, so what I want to do with this is I am just going to drag this text into the library, and I am going to call it text.
Okay, so this could be anything on the stage. In this case, it's just some text. But I want to take this vector information and in the Properties panel, I want to cache this as a bitmap. So under display, cache this text as a bitmap. This happens at runtime. So this is exactly what I want to do and this is going to work out quite well. Well, for most of my game play all of the graphics are actually dynamically created.
They are pulled from the library, if you will, and what I need to do is I need to go ahead and cache these as bitmap as well. But again, they're added using ActionScript, so I need to use a code snippet to be able to make sure those are all cached as bitmaps. So right down here, there is Optimize Graphics. That's the code snippet I want to use. Just double-click on that, and it's going to add basically these two code snippets right in here.
Okay, so this first comment says, this is great for graphics that move along the x and y position or not at all, okay. So this is a perfect case, for instance, for my lasers and my player. So that's what I'd want to use this for, cacheAsBitmap = true. The second one down here--and I'll just hit Return a couple of times-- this is great for graphics that scale, rotate, or even maybe change their alpha or their transparency. So for this one, I actually want to use this for my enemies.
Okay, just like that. All right, so let's start with this first one right here. I am going to take this line, I am going to copy it, and I am going to go up to where my player is created. I should have, actually, a start player right up here. Scroll up a little more, right in here. Here's my start player, and I'm going to look to where these properties are added, because the CacheAsBitmap is basically a property, so that's what I want to add, and I am going to add it before the addChild, before I get to put onto the stage.
I am going to paste in that line. So for instance name here, that's going to be player, all right. So my player is going to be cached as a bitmap. Let's take a look. We have this LaserTimer.start (). That's referring to the laser timer timer class. So I'll scroll down, and that's right here. This function, line 171, right in here after the x and y, I want to go ahead and paste in that line, and for all the tempLasers, go ahead and cache them as bitmaps as well.
Okay, so those two are set up. Let's scroll down a little further, and I don't need these lines, so I am just going to delete those lines of code, and right in here I am going to go ahead and use this cacheAsBitmap matrix, and that's going to enable me to cache all the different instances of these enemy spaceships. So as they rotate, it's actually going to cache all of those rotations as a separate bitmap is what this does right here. But I still also need to include the cacheAsBitmap as well. So I am going to take these two lines, I am going to cut them, and I'll just delete those comments down there at the bottom.
Now let's scroll up where we have the makeEnemies. Here we are, makeEnemies, about line 229. Here is this function, right in here, sure enough. Here is that property for moving its x position, and I'll paste those two lines right in here. So again, I'm dealing with tempEnemies, so I am just going to copy that instance name and paste that in where it says instance_name_here, just like that. Okay, there we are, so even the tempEnemy as it rotates, because it will rotate as well, well, I want to go ahead and cache each one of those as a bitmap.
You might be asking, what do you do with animated graphics? Well, let's take a look, because there is that explosion as well. So here's this explosion, and I'll just double-click on it, and I'll zoom in on this explosion. You can see that this is vector data right here, okay. So this is just a vector line that I created in Flash, and I want to replace these actually with bitmaps. So the cacheAsBitmap or cacheAsBitmap matrix does not work on animated movie clips like these, because there's one frame, two frame, and then three frames.
So in this case what I want to do is I am going to create a new layer and I'm just going to import some bitmaps of that same explosion. So I am going to import to stage. In the graphics folder, I am going to select this Explode_1, and since these are all numbered, it's going to bring in all three of these, and that's exactly what I want. So this is PNG file that was created actually from this original vector. I just took a screenshot, cropped it down, and saved it as a PNG file with transparency, and I'm going to select Open, and it's going to bring in this entire sequence.
So those three images, I'll select yes, and there they are. I can first kind of space these out, each keyframe, so these explosions appear at the appropriate time. And not only that, I can get rid of these two layers below just by deleting them, clicking that trashcan, and deleting those two layers. The last thing I am going to do is I am just going to go ahead and like center those graphics kind of like that. All right, there we are, and I'd say we're good to go.
Lastly, here are my PNG files. I can go ahead and move those into the assets folder, just to do a little bit of cleanup. But again, for animated graphics you might consider using bitmaps by importing them; otherwise, you can use cacheAsBitmap or cacheAsBitmap matrix, just as long as you are basically taking most of your vector content that's moving around a lot and converting it to bitmap. It really is going to help performance on a device.