New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Optimizing ActionScript

From: Building Mobile Apps for Multiple Devices with Flash Professional

Video: Optimizing ActionScript

When you're optimizing your app you not only need to optimize the graphics, you need to take a good look at your ActionScript to make sure that's optimized and performing well. So, I am going to go ahead and open up my document class and I am going to hit F4 to take a look at this code and when it comes to optimizing your code, you want to look out for two things, you want to look out for any way you can make the code more efficient, so getting rid of any redundancy, as well as taking a look at any ENTER_FRAME Events or any timers that constantly are being executed or called.

Optimizing ActionScript

When you're optimizing your app you not only need to optimize the graphics, you need to take a good look at your ActionScript to make sure that's optimized and performing well. So, I am going to go ahead and open up my document class and I am going to hit F4 to take a look at this code and when it comes to optimizing your code, you want to look out for two things, you want to look out for any way you can make the code more efficient, so getting rid of any redundancy, as well as taking a look at any ENTER_FRAME Events or any timers that constantly are being executed or called.

So as I scroll down, I can see all those variables, everything looks pretty good. I have these EventListeners which I need those. As I scroll down, I can see that this stageHeight and stageWidth is being used plenty, and it's even being used further down on my code as well. Well, I want to actually replace this with a variable. So I'll go ahead and just copy this and I'll scroll up to where all my variables are, and I'll add a new private variable and I'll call it centerHoriz, just like that and I'll make sure it's a number.

So I've datatyped it as a number, now I need to go ahead and define it and I am going to define it right down in here, within this setPosition function. So in here, centerHoriz is equal to the stageWidth divided by two, just like that. All right, anywhere this is being used can now be replaced by this variable. So I'll take this line, highlight it, then I'll select Find, Find all Instances of that and Replace it with centerHoriz, just like that, hit Replace All.

Goes through and finds 10 different items, so it doesn't have to make those 10 calculations anymore. It just has to make it once. So I'll click OK, close that. Notice how it will replace your original. So you do need to bring that back, so just paste it again and that's set up and look there is that variable in all of those places. Next up, I want to go ahead and make something that is going to be equal to the stageHeight. It's going to be a variable. So again, take that scroll up and right up here, private variable and heightOfStage is what we'll call this, so Number as well.

For this heightOfStage I'll just copy that variable name and I'll scroll down right in here, I'll paste it in and make that equal to the stage.stageHeight. So I'll copy that and paste that in. So that's looking good, I can now go ahead and replace all instances of this by clicking the Find button, right down in here, heightOfStage, Replace All. That's great! It replaced 19 different items.

So again, my code is getting really efficient. But right in here I need to bring that back for the heightOfStage, make sure that's equal to the stageHeight. All right, that's looking good; I am feeling good about all of those instances. I can scroll through and see that it's replaced in some other places as well, but I am going to go ahead and take a look at some other cases. In fact, right up here with these EventListeners, I have this intro frame ENTER_FRAME Event. And this is really what moves that monster around, okay, so if I click on the Monster.fla, it moves him around the screen.

But he's only on in the intro, then he actually just disappears. So I need to go ahead and remove that EventListener and add it only when he's on this Home Screen. So go back in here, and I will copy this. It gets added when the game is first created. And when the game is created, it basically happens when the board gets created. So I'll just do a quick search and I'll jump down to basically createBoard. Okay, so I am just going to use this to find where that's at.

So whether you click on Easy, Medium or Hard it creates that board. So right in here, after the background is done being animated off or animated off to the left hand side, right in here before my monster disappears, I want to go ahead and add a removeEventListener. So remove that ENTER_FRAME event, otherwise it's going to continually fire at the frame rate of the movie. So removeEventListener, that looks good.

Now that will keep it from moving, but I need to activate it again when you go back to that intro screen, so basically when the game is reset. So I'll just do a Search and I'll search for a reset, right down here where the screens are reset, where this monster is created again I want to go ahead and paste in that EventListener, just like that. Alright, so now I don't have to worry about that ENTER_FRAME firing off while the game is in play and potentially slowing it down.

So I'll save this and I'll test this out on my desktop just to make sure everything is good. And there it is, I'll scale this up, you can see everything still scales fine, based on those new variables that I've created and this guy of course moves around, but he will actually disappear and the ENTER_FRAME will get deleted as well, anytime you click on any of these buttons. It animates, he disappears, perfect, works great! So again, the point is to take a hard look at your ActionScript, look for any ways you can make it more efficient and be really mindful of any ENTER_FRAMES or timers that might be executing.

And you'll notice, once you optimize your code, your game actually will perform a good deal better.

Show transcript

This video is part of

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.