Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I was attempting a 'Hit the X' game (e.g Toss the turtle, spank the monkey) in which an uncontrollable object travels across the screen in hopes of hitting certain objects and avoiding others.

My dilemma is how to efficiently create and destroy these objects with respect to the character - that is, if the player is at, say, (90,34), all objects whose x position is less than 90 are garbage collected while more objects are generated at x > 90 or 100.

The only solution I could think of was to: randomly populate a tree that organizes by X values at the start of each round and to simply trim off the leaves at every so frames - if the player hits a certain spot, in which the tree is looking scrawny, start adding some more random objects on.

But this seems inefficient and unwieldy. Does anyone have any suggestions?

share|improve this question
Unless you'll be creating/destroying 100-500 of these per animation step, I doubt you'll see any performance problems with a straightfroward implementation (e.g., without optimizations). What is your target platform and how many objects are you dealing with? – Todd Lehman Feb 27 '12 at 6:20
You might want to have a look at polygonal datastructures. It's a library with implementations for all commonly used data-structures, even implementations for object-pools, which is probably what you want. – bummzack Feb 27 '12 at 7:41

Unless I am misunderstanding the situation you are asking about, this seems like premature optimization to me. I would say implement whatever technique is most straightforward for you. You can optimize it later if you start having performance issues, but for such a simple game I wouldn't expect to have a bottleneck with the creation of random objects.

share|improve this answer
Agreed... I'd venture to say you've spent more time thinking through this than you will ever save in CPU time. – stephelton Feb 27 '12 at 4:01

What you want to do is have an boolean active property on your objects that determines whether or not they get drawn/can be affected.

You then create a pool of the objects (i.e. define and initialize an array of inactive (active == false) objects as large as the most objects you'll have active at one time). Then, when you want to spawn a new object, find an active == false object in the array and set it's properties and then set it to active = true. When you want to get rid of objects, loop the array to see what objects have an x value less than 90 (or whatever) and set their active = false.

This means you won't have any garbage to collect and should help your game run faster.

Actually, if you used a stack (here's an implementation for actionscript) for the inactive objects and an arraylist for the active objects, then you wouldn't have to loop anything to figure out if you have available inactive objects. When you want a new object, you just pop it off of the inactive stack, set its properties and add it to the active list. Then, the game only draws items from the active list. When you want to remove something, remove it from the active list and push it onto the inactive stack.

share|improve this answer
The stack is great for inactive objects, but I wouldn't recommend it for active objects, since it restricts the order in which the objects can become inactive (to what is a very unlikely order - newest first). – yoozer8 Feb 27 '12 at 3:23
@Jim - Ah good point. I'll update my answer. – Richard Marskell - Drackir Feb 27 '12 at 3:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.