1
votes

Im almost finish with my game made with Adobe CS5.5 AIR 3.0 for iOS. Im trying to optimize the code because of some issues with preformance.

Im kinda new to programming and just gonna ask some questions.

  1. Im using a class I named "GlobalVariables". I use this for like have a "Speed", that I can reach everywhere. So if im on the main line or, in a class I can type "GlobalVarialbes.Speed" and then change the speed. Is that a good way of handle stuff? Or does it get heavy for the preformance to do this? I just kinda of many of these "globalvariables".

  2. One REALLY annoying thing is that when objects move, I really need the FPS to be 60 FPS. Because if the FPS is around 30-40, the objects doesnt move at the same speed, and the objects seems to be alot closer the each other to... This is really annoying. I just use object.x += GlobalVariables.Speed (Speed is maybe 4)... Any suggestions here?

  3. If I use a background it actually takes alot of the FPS from me. Around 7-10 fps. How can I make a background picture less preformance eating?

  4. General tips maybe? I use the GPU mode on the iOS devies :)

EDIT PLEASE ANSWER THIS: My friend told me to use "alot" of if's because, then It would be easier for the preforance to actually see if it even gonna read the following code or not. :)

4

4 Answers

6
votes

Going through you list one at a time. (Its really 4 questions at least)

  1. Im using a class I named "GlobalVariables". I use this for like have a "Speed", that I can reach everywhere. So if im on the main line or, in a class I can type "GlobalVarialbes.Speed" and then change the speed. Is that a good way of handle stuff? Or does it get heavy for the preformance to do this? I just kinda of many of these "globalvariables". Using global variables and function calls can take some time. If you use the variable more than once within a method, you can optimize slightly by caching the value to a local variable. But be sure to test the outcome, as caching the object if it is not a primitive value can cause a "new Something()" constructor to be called which is very slow.

    One REALLY annoying thing is that when objects move, I really need
    the FPS to be 60 FPS. Because if the FPS is around 30-40, the
    objects doesnt move at the same speed, and the objects seems to be
    alot closer the each other to... This is really annoying. I just use
    object.x += GlobalVariables.Speed (Speed is maybe 4)... Any
    suggestions here?
    

Change the way you think about speed, from pixels per frame, to pixels per second. Then use getTimer() each second to find out what percent of a second has passed since the last rendered frame. I put some example code in this thread here: Frame skipping on Flash

If I use a background it actually takes alot of the FPS from me.
Around 7-10 fps. How can I make a background picture less
preformance eating?

Backgrounds, and drawing in general will be costly operations. Use cacheAsBitmap if the background does not scroll. Be sure the background contains no child layers if possible. Use a bitmap and not vectors. And keep it as small as possible - do not have extra bits off screen. Also- make sure the background is not being scaled. Turn off bitmap smoothing too. Try setting you stage quality to StageQuality.LOW, or StageQuality.Medium and see if that helps. ==UPDATE== Now that AIR 3.2 is out and it supports OpenGL on iOS and android, use that feature. Its called "stage3D" in flash and it is really the only good way to do your graphics for mobile with flash. For 2D graphics out the starling framework. http://gamua.com/starling/ It's the same engine used by Angry Birds on Facebook. So its real-world tested and production ready. (And its open source!)

General tips maybe? I use the GPU mode on the iOS devies :)

General tips: use bitmaps for everything. Avoid transparency whenever possible. Do not use filters, or blend modes. Avoid Alpha tweening. Avoid creating new objects - recycle commonly used objects, especially display objects. Remember that adding and removing children is also costly. If you have a bunch of bullets in a re-usable bullet pool, you can set them to visible=false. Then visible=true when you re-initialize that bullet object. Try a search for "AS3 Object Pooling"

Use Vector instead of Array.

And Finally: Test and re-test. Measure the time of each function call. Optimizing flash is a deep and rich topic - its one that you will never exhaust. In part because your mileage will vary from project to project. Here is a good link to get you started on learing Optimization techniques for AS3 http://drawlogic.com/2009/05/22/as3-flash-efficient-code-techniques-vectors-in-flash-10-faster-jpeg-encoding-other-optimization-notes/

0
votes

1 Objects are passed as reference so a change on that object in one class will reflect anywhere that object was passed to. You also might look into the Singleton pattern if you want class accessible globally. One last suggestion would be to use a config.as using CONST declarations just instantiate a new instance of it when you want data from it

2 This is a draw-back from using the enterFrame event. Change this to a timer and get rid of the enterframe. Also if you have heavy data manipulation that changes a display object multiple times a frame rate you might want to look into UIComponent and study up on the invalidation methods. This will allow the display to only update on redraw(framerate)

3 Have you tried to cacheAsBitmap

4 I am not sure how much of GPU is actually utilized as it is still pretty new.

0
votes

You've already received a few answers that are very good for adjusting how your brain works to help in performance. I'm going to simply add a link to a site that can help make these tips concrete. *-bit rocket, if you haven't been there has one of the best blitting and general game tutorials that will teach you to write efficient game code on any system.

When I was where you are now in programming, I stumbled across the site, and the asteroids tutorial in particular will help answer these questions.

http://www.8bitrocket.com/2008/1/20/Tutorial-Using-Flash-CS3-and-Actionscript-3-to-create-Atari-7800-Asteroids-Part-1/

It'll also prepare you for the next version of air that will have the stage3d api built in. you'll get a massive performance increase. At that point, I'd probably recommend switching to starling framework- which uses all the techniques you'll read about in the tutorial I'm linking you to, but behind the scenes.

http://www.gotoandlearn.com/

has a decent intro tutorial talking about that.