Spawning objects dynamically

Something I had been toying with was a means of simply spawning explosions at any given x and y location during the game. This is particularly useful for representing a boss’s death sequence. I had already implemented pain frames for bigger adversaries so it made sense to give the player complete satisfaction by visually destroying the boss.

Screenshot with spawned elements

Screenshot with spawned elements

How to implement it was a two way decision. Creating the IMG object on the fly in the code is simple enough. Handling the animation was also simple. I set it’s canAnimate flag to 1 and supply an array of animation frames in the object’s instantiation. But handling the sprites in the game loop was something I hadn’t considered before.

For other dynamically created objects it’s actually smoke and mirrors. I define the objects at game load and simply recycle them. But for the explosions, well, there could be hundreds of them before I get to game load again. So I decided to have an open-ended array that increased in size as I required it to.

On playerDeath or levelUp I simply reset the array. (Note. Initially I used array.splice(0) and then even easier new Array()) But something is lurking since IE displays an empty image element on reset.
Note: The ’empty image’ is fixed by simply giving each new element a completely unique ID. I was also recycling the IDs that had been allocated when the object was first created. Common sense really.

I’ll find and fix the bad code but the important thing is that the spawned explosions look fantastic.
Strictly speaking they’re not explosions more puffs of smoke. I put them all over the player’s hovertank on every rocket launch but it soon became a mess of “puffs of smoke”.

Now that the framework for dynamically creating and handling special effects is proven I will probably use the same method to implement other incidental features such as bonus items and random aliens.

Post a comment or leave a trackback: Trackback URL.


  • Kris  On September 21, 2008 at 3:20 pm

    Good work! You should also count the levels. The final fight with the big angry boss will be also the nice feature… :)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: