0
votes

im making a flash shooter game and ive encountered one problem. When there are a lot of monsters on the stage which are visible by player, the game starts to lag. In my opinion, its due to Event.ENTER_FRAME (each enemy instance has it) where z-sorting, enemy movement, updating other stuff like health is done. Since things like theese, cant be done each second or at similar time interval,im using ENTER_FRAME. My question is, how can i have many instances of the enemy in my game and still dont have it lagging. Ive done optimising in all over the code and if im not mistaken,big ammount of enemies is the performance bottleneck here.

Question me if i wasnt clear; to see the game go to http://ernyz.lhosting.info/bandymas.html or if you want to see the code,i will be able to put it here,just ask :)

2
You should post some code. There might be some easy optimizations. Also, how many enemies are "many"?Jonatan Hedborg
Well, 10 active enemies per room make this game lag, so i think, that can be counted as many. Active are those, who are in the same room with player, others 'sleep' and do nothing. Heres whole enemy class <sendspace.com/file/iqztfp>Ernyz
var penis:Number = 0; ... what?kapex
Whoops, i make idiotic var names when im in a bad mood to cheer myself up, ignore that :DErnyz
Why couldn't the updates be done each second? You could create a timer and execute your update code when it fires.sean

2 Answers

5
votes

Having an enter frame events for each instance is most likely the problem. A single event where you loop over all instances and do actions is usually faster.

There shouldn't be much for you to change: Instead of adding the listener to each enemy, add only one listener to the stage and call the enemies' update functions.

class Enemy {
    function update(e:Event) { /* ... */ }
}

class Main {
    function onEnterFrame(e:Event) {
        for each (var enemy:Enemy in enemies) {
            enemy.update(e);
        }
    }
}
1
votes

From my experience, unless you're doing something very wrong, flash rendering pipeline is what takes the most time of your application. And since you get more enemies, you get more MovieClips and more complex rendering.

But having one ENTER_FRAME event for each object is indeed a big overhead that can be easily avoided.

A good practice before optimizing your code is to actually run it through a profiler. I don't believe the actual Flash program has it, but Flash Builder surely does. If you post us a screenshot or a log of the game being profiled, we can be of more assistance.

By quickly playing your game, I've seen that all your enemies are a bunch of graphics with a bunch of gradients, therefore costly to render. Have you tried setting the quality to low? Does the lag go away?