#### Archived

This topic is now archived and is closed to further replies.

# How to fix this problem with my Bullets in my game?

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

## Recommended Posts

My game is an asteroids clone and this is the problem: When I shoot for the first time, it fires all the bullets that it can (up to 12), then when I fire again, I get 11, then 10, and so forth until I can't shoot anymore. There is no asteroids yet, so I'm not worrying about if there's a bullet missing in between the bullets. Here's the code, it should work, in my opinion (sorry, I don't know how to make that fancy box thing) In Game_Main in the update shots part:

for(int i = 0; i < Player.MaxShots(); i++)
{
if(Player.Bullets[i].UpdateShots() == 1) //0 if it have lives left [/i]

{
Player.Bullets[i].Clear(); //resets the bullet to clear [/i]

Player.SubShot(); //subtract 1 life from the bullet

}
Player.Bullets[i].DisplayShots(Player.Type); //displays shots [/i]

}

in the Player class for the shots:

class TShip
{
public:
...

void MoveShip(int&, int&); //includes shooting

...

int Shots()		{ return NumOfShots; }
int MaxShots()	{ return MaxShot; }
int ShotLife()  { return BulletLife; }

void SubShot() { NumOfShots--; }

...

TBullet Bullets[MAXSHOTS];
int Type; //which type of ship it is, behemoth, corvette, or cruiser, affects the bullet's color

int Rapid; //how fast it can shoot

private:
...
int MaxShot; //the maximum shots it can have on screen

int NumOfShots; //how much shots is on screen

...
int BulletLife; //how long a bullet stays on screen for when firing a new bullet

};

TShip's define:

void TShip::MoveShip(int& direction, int& row)
{
static int count = 0;

if(KEYDOWN(DIK_RIGHT))
{
(rotates ship counterclockwise)
}
else if(KEYDOWN(DIK_LEFT))
{
(rotates ship counterclockwise)
}

if(KEYDOWN(DIK_UP))
{
(acceleration)
}
if(KEYDOWN(DIK_SPACE))
{
static int count2 = Rapid; //for rapidity of firing

if(count2 >= Rapid && NumOfShots <= MaxShot)
{
}
else
count2++;
}
}

and in the Bullets class:

class TBullet
{
public:
void Clear(); //clears the shot

void Shoot(int , int , int , float , float , int); //the ship shot

int UpdateShots(); //move the shots and subtracts from life

void DisplayShots(int); //displays the shots

float returnXpos() { return shotX; }
float returnYpos() { return shotY; }

private:
float shotVel[2]; //velocity for shots

float shotX; //x location of shots

float shotY; //y location of shots

int CurrentLife; //how long it can stay

};

the Bullet class' defines:

void TBullet::Shoot(int direction, int x, int y, float xvelocity, float yvelocity, int life)  //Shoot in the direction

{
shotX = x;
shotY = y;

CurrentLife = life;

{
(sets velocity and location of bullet)
}
}
{
CurrentLife--;
if(CurrentLife == 0)
return 1;
else
{
shotX += shotVel[0];
shotY += shotVel[1];

if(shotX >= SCREEN_WIDTH+17)
shotX = -16;
else if(shotX < -16)
shotX = SCREEN_WIDTH+16;

if(shotY >= SCREEN_HEIGHT+17)
shotY = -16;
else if(shotY < -16)
shotY = SCREEN_HEIGHT+16;
return 0;
}
}
void TBullet::DisplayShots(int Type) //displays the shots, type affects the color

{
(displays bullet)
}
void TBullet::Clear()
{
shotX = -20;
shotY = -20;

shotVel[0] = 0;
shotVel[1] = 0;
}

Thanks for your help. Also, thanks for telling me how to make boxes [edited by - Zeraan on July 26, 2002 5:54:12 PM]

##### Share on other sites
Use code tags like this: [ code ] and [ /code ] with the brackets closed, of course.

##### Share on other sites
Actually the [ source ] and [ /source ] tags are better, they highlight and put the code in a smaller window so as not to fill up the whole screen.

~~~~~~
http://www.goatsoft.com
Try my Tri-Towers clone

##### Share on other sites
Well, I see a few problems... One thing that bugs me is that you have not properly encapsulated the shot logic, and your functions have misleading names. Instead of looping through all the shots and updating them one by one in your main game loop, all of this should be handled internally to the Ship or player class.

As for the actual bug, I suspect it has something to with the following code:

if(count2 >= Rapid && NumOfShots <= MaxShot)
{
//(Makes a new bullet) [slightly edited]
}

Note you are checking for NumOfShots <= MaxShot. So when NumOfShots == MaxShot, the code will execute, and a new bullet will be created... give you MaxShot+1 number of bullets. Then, when you are actually updating the bullets your conditional check looks like this:

for(int i = 0; i < Player.MaxShots(); i++)

So, I think you are off by one here. Just a guess though. It should be much easier for YOU to find the bug. Just think through the logic of the code... ask yourself, "what stops a new bullet from being created?" Well, the answer to that is the if statement above. So, we know that either count2 < Rapid or NumOfShots > MaxShot. So, when does count2 get modified? When does NumOfShots get modified? Which is actually causing the failure? Follow the game logic and think it through.

##### Share on other sites
Thanks for your help, but that isn''t it. I decided to program a little message thing so I can display the number of shots I have on screen out of max shots for debugging. Thanks for taking the time.

##### Share on other sites
Doh, it was that! After I added the message thing, I got 1 over the MaxShot. Now that''s fixed (I don''t know exactly why, the <= to < didn''t work before, now it does???), I got another problem, but I know why it is that, but I don''t know how to fix it.

its like this, if I fire 12 shots out of 12 max, and then some bullets disappear, for example 5, the num of shots is now at 7, and when I fire again before all bullets disappears, the new bullets are written on top of the old bullets, and reducing the number of bullets I can shoot, and shortens the life of the old bullets.

I know why this is like this because I used Bullet[numOfShots}, so I need to do something like this when a bullet disappears.

Bullet = Bullet[i+1];

But since this is class, I tried to overload the operator = and it worked, but when I exit the program, it freezes the computer.

this is the code (its a lot shorter than the previous one heh):

  //in Game_Mainfor(int i = 0; i < Player.MaxShots(); i++)		{			if(Player.Bullets[i].UpdateShots() == 1)			{				Player.Bullets[i].Clear();				for(int j = i; j < Player.MaxShots()-1; j++)				{					Player.Bullets[j] = Player.Bullets[j+1];				}				Player.SubShot();			}			Player.Bullets[i].DisplayShots(Player.Type);		}//in Bullet classTBullet operator=(TBullet& rhs) {		shotVel[0] = rhs.shotVel[0];		shotVel[1] = rhs.shotVel[1];		shotX = rhs.shotX;		shotY = rhs.shotY;		CurrentLife = rhs.CurrentLife;		return *this;	}//this is in the class itself, not an external function, I don''t remember how to do that.

I don''t see anything wrong with that.

##### Share on other sites
Never mind... Found the problem. Now this unlocked one more bug, but hopefully this is the last one for the bullet
Asteroids, here I come!