• Advertisement
Sign in to follow this  

Creating multiple objects

This topic is 4306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to create multiple instances of bullets for a 2D side scroller, however, the code is driving me crazy. I can either get one instance to appear and move, or if I try and create more than one instance, the program throws a wobbly unless I include all calls to my bullet class and the initialisation in one for loop, like below: Can anyone help me? I've tried every possible combination and feel there's only one more hurdle, lol
//Pointer to the Bullet class
CBullet **Bullet = new CBullet*[10];

//Player constructor
CAvatar::CAvatar() {

	x = 0;
	y = 0;
	bulletdx = 0;

	CurrentState = 0;
	TotalState = 0;

	AnimationSequences = NULL;

	TransRed = 0;
	TransGreen = 0; 
	TransBlue = 0;

}

//The shoot function for the player
void CAvatar::Shoot(int x, int y, int state, bool alive){

	for(j=0;j<=9;j++){
		Bullet[j] = new CBullet();
		Bullet[j]->LoadImage("Bullet.bmp",true,255,255,255);
		alive = false;
		cout << "\nLoaded the image\n";

		x = bulletdx;
		bulletdx+=10;
		Bullet[j]->Addxpos(bulletdx);
		Bullet[j]->Draw(state,bulletdx,100);
	}
	if(bulletdx >= 400){
		bulletdx = 200;
	}

}

The bullet class
void CBullet::Addxpos(int dx) {

	x += dx;

}

//ignore the Draw function, the state is always 1.
void CBullet::Draw(int state, int x, int y){

	if (state == 1){
		CBullet::DrawImage(x,y);
	} else
	if(state == 0){
		CBullet::DrawImage(-10000,-1000);
	}
}

Share this post


Link to post
Share on other sites
Advertisement
Why are you doing this?

CBullet **Bullet = new CBullet*[10];


and not this:

CBullet* bullets = new CBullet[10];

Dave

Share this post


Link to post
Share on other sites
You can check out how I handled multiple objects in my showcase submission.

Basically I did this:

First I used std::vector to hold my rocks rather than a raw array since it's easier to add and remove entries as needed. When I wanted to add an entry I would create a rock object, set its parameters, and then just push it onto the vector. Similarly, if I wanted to remove it I just used erase().

When I wanted to cause all the rocks to move I iterated through the vector and updated the members of the rock object.

If you choose to look at the code, this is all in the ProcessMovements() function

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
Why are you doing this?

CBullet **Bullet = new CBullet*[10];


and not this:

CBullet* bullets = new CBullet[10];

Dave


Sorry Dave, forgot to add this in the Avatar constructor:


CAvatar::CAvatar() {

x = 0;
y = 0;
i = 0;
bulletdx = 0;

CurrentState = 0;
TotalState = 0;

AnimationSequences = NULL;

TransRed = 0;
TransGreen = 0;
TransBlue = 0;

for(i=0;i<=9;i++){
Bullet[i] = new CBullet();
Bullet[i]->LoadImage("Bullet.bmp",true,255,255,255);
}
}

Share this post


Link to post
Share on other sites
The question stands: Why make an array of pointers to objects rather than just an array of objects?

For that matter, since you know the array size at compile time, why are you dynamically allocating it? If you *do* want a varying number of the things, have you considered std::vector?

Have you noticed that every time you .Shoot(), you replace all of the bullets with new ones and don't make any attempt to clean up the old ones?

Oh, and why are you passing 'x' and 'y' to Shoot()? They don't get used within the function (Ok, you change 'x', but then don't do anything with that value). Were you hoping to modify a CAvatar member called 'x'?

Also, why draw the bullet at that strange location when it's in its 'dead' state? Why not just *not draw it*?

Also, the bullets should be able to "update" themselves in the same way that it can "render" themselves. Right now it looks like you call 'Shoot' every frame, which doesn't really make much sense. What you want to do is iterate over *existing* bullets (not create new ones) and tell each to 'update', where the update adds the bullet's velocity to its x position.

Also, is that a global Bullet array or a CAvatar member?

Share this post


Link to post
Share on other sites

char** argv = char* argv[]
So essentially you're basically declaring that for your bullet array? No that I agree with it, but from I am reading that's what is happening there. I would go with the other guys said it simply looks much cleaner to my eyes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement