Sign in to follow this  
uncle_rico

Creating and destroying new objects on the screen

Recommended Posts

Hello there. I'm currently writing a simple 2D collision detection demo with "ball" sprites. It starts off with just one ball, that appears in the middle of the screen and shoots off in a random direction. By hitting the space bar, you can create a new ball, which also appears in the middle of the screen and shoots off in a random direction. You can keep on creating new balls as many times as you can hit the space bar. So, the problem I've run into is this. I want these "balls" to be global. I created a Ball class which stores all the info necessary for the ball. So, I supposed I could just declare the following globally: Ball ball[8]; And then just give the Ball class an on/off property that I can use to hide the balls that shouldn't be there. The problems I can't seem to get around are: a) I have to create a maximum number of balls (8 in this example). What if I want the user to be able to create as many as they want? b) It seems like a waste of space to declare balls that aren't yet in use. The higher the maximum, the more the waste. Item (b) seems especially problematic when I consider what it means for larger games -- does one have to declare every object that could possibly appear in the game right away, knowing that most everything declared will be "hidden?" Is there any way around this?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this