Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Optimizing your ActionScript code is just as important as optimizing your graphics. That's what I want to do now. I am going to go into my ActionScript code. Here it is, and quite frankly, there's a lot going on. I need to optimize this, and what that means is I need to go ahead and look at ways that I can condense down some of this functionality, whether there's enter frames that can be condensed down maybe into one enter frame, or if there are for loops that can be condensed down as well.
I also need to take a look at expensive processes, and when I say expensive processes, I mean, more than anything, enter frame events. Okay. So I am going to go ahead and take a look at my enter frame events, and I want to make sure there are not a lot of them, for one, okay. So as I scroll down, about line 62 there's this Enter_Frame game loop, and this constantly checks the game state we are in. And that's fine, but what I am going to do is I am going to select this word and I am going to do a Command+F, or Ctrl+F if you're on a PC, and I am just going to do a Find Next.
All right, here is another one. Here is the enter frame for moving the player. Well, I need to do that as well, so that one is okay to keep in as well. Find Next, and again, this is a remove event listener. I, in general, only have two enter frame events: one for the game loop and then one to move the player. So that looks pretty good. But I want to go beyond an enter frame event, just looking at how many there are, because I need to be aware of how often this enter frame event is firing off, and it does so at the frame rate of the movie.
So I am going to pin my code, and I am going to take a look over here in my Properties panel. The Frames Per Second for this document is 24. I might consider dropping that down to, say for instance, 20 frames per second. Okay, it should still play smooth and look pretty good while not being that taxing on the mobile device. If I drop it down to say 15, it's going to get a little more choppy, so I think 20 is pretty good for this project. Okay, so the enter frame looks good. I am going to go ahead and just take a quick glance at some of the other code as I scroll down.
Let's go ahead and take a look at, let's say for instance this timer event. This works out pretty well, and these are really good to use because I get to determine, independently for each timer that is, how often it executes, and that one executes every half a second, so that's pretty good. I might want to take a look at maybe moving my enemies or even, like, testing the collisions for instance. Let's take a look at these testing collisions. 284, that's where the function is, and I am noticing that this loops through the enemies array.
Okay, it loops through the enemies array, because it checks to see when an enemy and a laser collide. So that's what this test for collisions does, okay. But I might consider combining this with something else. In fact, if I scroll up, I can see that with this moveEnemies there is this same for loop. So I want to go ahead and combine these two. In fact, I want to get rid of this test collisions altogether. All right, so in order to combine this, first thing I am going to do, since I am not going to need this anymore, I am going to remove that enemy for loop.
Anytime you remove an opening curly brace, you need to remove the closing curly brace as well. Same thing for this function as a whole. I can remove it, remove that closing curly brace, and I don't need this tempEnemy anymore either, because it's going to be up above. There we are. So this is what I have it condensed down to. So this is a lot better. I have eliminated that for loop. I can go ahead and take this code, and in fact, I did notice one more thing right down here for this break enemy.
Well, that doesn't exist anymore. It's not in that for loop, so I can remove that as well. So again, I am going to take this chunk of code and I am going to cut it, and right up here within this for loop, right in here, after this closing curly brace right here, hit Enter, and this is for the Make Explosion, and paste that in. Lastly, I will go ahead and click my Auto Format button, just to make sure everything nests in nice and neat, and I'd say that looks pretty good.
But keep in mind that I did go ahead and I removed that test for collision basically. So right up here in this playGame, since that testCollisions function no longer exist, I need to remove it as well. And anytime you're removing a function, it's always good just to select it and again just find it, so Command+F or Ctrl+F and just click Find Next, just to make sure it's not anywhere else, if you're removing things. So this Find and Replace is going to be really helpful when you start condensing down your code.
All right, so that's fine. I can remove that, and again, I am just simplifying this code. And again, I can go in and kind of clean it up and make sure everything is pretty squared away. A lot is going on here, and I'd say this is pretty powerful. Know that it actually does not execute any of these commented lines, so I don't need to worry about that. Trace statements, I am not going to worry about those either. In general, I think this game is good to go. I will do a quick test movie in Flash, just to make sure everything plays correctly.
(explosions) All right, so everything was playing well. It seemed pretty smooth. I liked how it was working, but the next step is to really try this out on a device. Now, you are not going to get ideal performance compared to a Desktop, but again, you want to make sure you optimize all your graphics and your ActionScript code before you release it or publish it into the Android marketplace.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98327 Viewers
61 Video lessons · 85625 Viewers
71 Video lessons · 69566 Viewers
56 Video lessons · 101898 Viewers
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.
Your file was successfully uploaded.