I am working on creating a game with C++ and, up until now, I have only really used other more simpler languages to create games. I am having a problem on figuring out to go about creating multiple objects of the same type, have them update themselves, and delete themselves when the time is appropriate.
What I need is for my game to create asteroids that spawn on the right side of the screen, move towards the left, and continue until they collide into something or go off-screen (at which point the asteroid object will be deleted). Here is the class I created:
class CAsteroid {
public:
Asteroid();
~Asteroid();
void updateAsteroid();
void destroy();
private:
int x,y,frame,size,id;
float xVelocity;
};
Now I know I could create an array that holds each asteroid. However, I don't want to do that for a few reasons (mainly because it seems like the easiest way as I know how to do it, but I want to this to be a learning process for me so I can use this method on other things such as bullets in the game). Also I don't wish to be tied down to a limited number of asteroids.
I assume that I will need to use a pointer, but I'm not sure how to go about this. I have questions like this: How do I get the pointer to iterate through each current asteroid that exists to have it update itself without going out of bounds? How do I get the asteroid to delete itself? If there is a way better than using pointers, what is it?
Based on what you're saying, you have incorrect design ideas.
Your asteroid class would only worry about being an asteroid. It wouldn't worry about creating and destroying itself. It also wouldn't worry about iterating over to other asteroids.
What you want to do, is you want to have a class that has a broader scope. One which "owns" all of the asteroids. This can be a "World" or "Game" class or something similar.
In that class you would have a container of all the asteroids. The container you might be familiar with is a vector -- but it's not ideal for this situation because you don't need to access the asteroids randomly -- and you will need to erase items in the middle of the group quickly. So a list is probably better.
Once you have your list of Asteroids, you would iterate through it like any list, and remove elements when necessary. Note again the Asteroid class would not remove itself. At most they would signal that they should be removed, and the World or whatever class would actually remove it. The world is the "owner" and therefore should be responsible for creating/destroying the asteroids.
class World
{
std::list<Asteroid> mAsteroids; // all the asteroids in our game world
Bullet mBullet; // a bullet (for simplicity in this example, I'm assuming there is only one bullet)
void Update(); // called to run game logic -- typically once per "frame"
};
//....
void World::Update()
{
// update / move the bullet
mBullet.Update();
// Update all the asteroids. If our bullet hit any of them, destroy them:
std::list<Asteroid>::iterator i = mAsteroids.begin();
while(i != mAsteroids.end()) // loop once for each asteroid
{
i->Update(); // update / move the asteroid
if( DoObjectsCollide( mBullet, *i ) ) // do the bullet and this asteroid collide?
{
i = mAsteroids.erase(i); // then destroy the asteroid by removing it from the list
// and move our iterator 'i' to the next asteroid
}
else // otherwise (they didn't collide)
{
++i; // simply move to the next asteroid
}
}
}