In my game, you can cast a spell that causes all of the enemies on screen to catch fire. There can be as many as 40 or 50 enemies on the screen at once. Currently, there’s a perceptible lag (about one second) when you tap the spell button, and I’d like to get rid of it. Here’s how it’s implemented:
- I loop through all the enemies on the screen
- For each, I create a new three frame animated sprite via display.newSprite(imageSheet, sequenceData)
- I set the flame sprite’s frame to a random index, so that all the flames don’t burn in sync
- I tell the animation to play
- I create two timers for each enemy: one that runs once for X seconds, after which the flame disappears, and a second that runs repeatedly every 100ms for the same total duration X, dealing damage to the enemy over time.
- I add an event listener to the flame, so that when the enemy dies, it calls removeSelf()
- In a separate table, I keep track of the relationship between the flame sprite and the enemy sprite
- In my enterFrame loop, I update the x and y of the flame sprite to match the coordinates of the enemy sprite (which has separate movement logic in the frame loop)
The lag only lasts for a brief moment, after which the enemies move smoothly around the screen happily (unhappily?) engulfed in flames. Does anything in the above stand out as a potential problem spot? I would have posted code, but it’s spread out across a couple of classes, so I thought this would be easier.
For what it’s worth, the enemies are physics objects, but the flames are not.
Thanks!