AS2 Best way to decrease lag when dealing with sev

2019-03-03 20:45发布

问题:

As the title states, I'm wondering what the best way to handle several movieclips on the stage at one time, each with their own onEnterFrame functions. Lets say we have 50 enemies on the screen at once, constantly playing a walking animation. The onEnterFrame function would consist of getting a direction, moving X/Y values, checking the distance between the player and itself, checking if it is in an attackable distance, etc. As you can imagine a lot of lag occures.

An type of a game where this problem is usually overcome would be in zombie games where you would have multiple zombies on the screen at one time.

What would be the best way to reduce lag when handling multiple movieclips like this?

回答1:

Each onEnterFrame listener creates new event objects, so the more onEnterFrame listeners you have the more objects gets created each frame. And in AS2 object creation is pretty slow.

So, one optimization to consider is to just register one onEnterFrame event and then from that "main" onEnterFrame loop through all enemies etc. and call an update function on each instance.

It is also a good idea from a code structure point of view since you now only have one piece of code running each frame. It makes it easier to see what is actually running each frame. And to pause the game you could simply stop the main onEnterFrame and the whole game would stop.