Optimizing my game...

12 posts

Flag Post

Hi there,

I’m new here. so I’ll introduce myself first! I am MarcianoSR and I love to program, especially with as3. I’m coming here to ask a serious question about optimizing the peformance in such a game, created in as3.

Ive got peformance problems when the game progresses. The game generates objects every certain time, when the game progresses, it will generate objects faster and faster. I think I cannot re-use the objects, but I remove all objects when I dont need them anymore, dispose bitmaps and remove event listeners. Still it lags more and more. What can I do to make it not lag? Is there a way to remove objects entirely?

Thanks for reading,

MarcianoSR.

 
Flag Post

Make sure you’re (i) removing them from any storage that you put them into (i.e. arrays, vectors or references to them directly from objects); (ii) removing them from the stage, if they’re display objects, and (iii) removing any event listeners that call back to them, as this also counts as a reference and stops the GC collecting them.

 
Flag Post

If the above doesn’t work, you may want to consider recycling the objects. It isn’t necessarily that easy to do, so I hope someone else will be able to explain it.

 
Flag Post

If you are using Flixel there is a class called FlxGroup which makes it easy to implement recycling of such resources.

 
Flag Post

Are you experiencing lag spikes at fairly regular intervals? That’s usually the garbage collector, although apparently not always (it has always been the garbage collector for me, but I’ve seen people claiming they were not generating any garbage and still had it). Are you experiencing more lag when more objects/effects are on the screen at once? That’s your renderer struggling to keep up with the load. Are you experiencing a general slowdown of the game while your machine starts roaring like a revved-up motorcycle? You might be using so much memory that your computer is caching to disk, which absolutely kills performance.

Try to diagnose exactly what is happening, where and why. FD allows you to monitor your memory usage, see how exactly how many objects of what type are in memory, etc… It’s the ‘SWF profiler’, the little alarm clock icon in the tool bar. I find it a great help to check that I’m not leaking memory or over-generating objects.

 
Flag Post

Thanks for all the replies.

I found the spot which made all the trouble. It was a single array which I forgot to empty. It got full off objects which I didn’t remove.

Thanks for thinking with me, I really appreciate it.

MarcianoSR.

 
Flag Post

Speaking of, Array.splice() is a cpu-heavy function. Use this instead:

myArray[theIndex] = myArray[myArray.length-1];
myArray.length--;

Provided that the order of the array is irrelevant.

 
Flag Post

And if you are running in a loop and are using the method Draco showed, make sure to do theIndex--; if you don’t want to skip over the object you just swapped.

 
Flag Post

How many objects are there on the screen at once? And what type of objects are they (Sprites, Movie Clips, etc)?

 
Flag Post
Originally posted by UnknownGuardian:

And if you are running in a loop and are using the method Draco showed, make sure to do theIndex--; if you don’t want to skip over the object you just swapped.

Depends on the order of the loop (did you start at the front, or start at the back?). I almost exclusively run through loops back-first these days.

 
Flag Post
Originally posted by Draco18s:
Originally posted by UnknownGuardian:

And if you are running in a loop and are using the method Draco showed, make sure to do theIndex--; if you don’t want to skip over the object you just swapped.

Depends on the order of the loop (did you start at the front, or start at the back?). I almost exclusively run through loops back-first these days.

Running backwards is usually faster (except on older machines) and it prevents skipping over on accident.

 
Flag Post

A way of recycling would be to include a renew and dispose function. The dispose function ceases everything from being triggered and renders the object invisible or moves it off the stage. The renew function makes the values reset to default as if a new object has been created. Any time you need to create an object you can check if one already exists that is in the disposed state and use that instead. Of course this method has its flaws.