Bullets acting up

Started by
2 comments, last by Laroche 21 years, 9 months ago
I can't seem to make my player shoot more then one bullet at a time in my asteroids clone. I've been working on this one for a while, and it's obviously a logic error, so I was wondering if anyone could check out these code snippets and tell me what I'm doing wrong. The basic idea of the 2 code snippets is simple: The first one is a request for a bullet, so it itterates through the bullet array and checks if there is a non-active one there. If there is, set it up and push it in to the EntityVector. The second one updates the bullets, and checks if they are outside the screen or not. If they are, it sets their Active state to FALSE, removes them from the entityvector, and resets them (put all the vars to 0 or NULL). EDIT: I realize a lot of code is abstracted away because of the classes, but I'm pretty sure it works. Just follow around based on the function names. It's the logic that concerns me.
    
void CManager::AddBullet(CPlayer *pPlayer)
{
	for (int i = 0; i < MAX_BULLETS; ++i)
	{
		if (BulletArray[i]->IsActive() == FALSE)
		{
			BulletArray[i]->SetActiveState(TRUE);
			BulletArray[i]->SetOwner(pPlayer);
			BulletArray[i]->PrepareSelf();
			pGame->AddEntity(BulletArray[i]);
			pGame->AddVisibleEntity(BulletArray[i]);
			return;
		}
	}
}
  
      
void CManager::UpdateBullets()
{
	for (int i = 0; i < MAX_BULLETS; ++i)
	{
		if (BulletArray[i]->IsActive() == TRUE)
		{
			if (BulletArray[i]->GetLocalX() < 0 - BulletArray[i]->GetBoxX())
			{
				BulletArray[i]->SetActiveState(FALSE);
				BulletArray[i]->ResetSelf();
				pGame->RemoveVisibleEntity(BulletArray[i]);
				pGame->RemoveEntity(BulletArray[i]);
			}

			if (BulletArray[i]->GetLocalX() > pGame->GetScreenWidth())
			{
				BulletArray[i]->SetActiveState(FALSE);
				BulletArray[i]->ResetSelf();
				pGame->RemoveVisibleEntity(BulletArray[i]);
				pGame->RemoveEntity(BulletArray[i]);
			}

			if (BulletArray[i]->GetLocalY() > pGame->GetScreenHeight())
			{
				BulletArray[i]->SetActiveState(FALSE);
				BulletArray[i]->ResetSelf();
				pGame->RemoveVisibleEntity(BulletArray[i]);
				pGame->RemoveEntity(BulletArray[i]);
			}
			if (BulletArray[i]->GetLocalY() < 0 - BulletArray[i]->GetBoxY())
			{
				BulletArray[i]->SetActiveState(FALSE);
				BulletArray[i]->ResetSelf();
				pGame->RemoveVisibleEntity(BulletArray[i]);
				pGame->RemoveEntity(BulletArray[i]);
			}
		}
	}
}
    
[edited by - laroche on June 26, 2002 12:03:57 PM]
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
Advertisement
Before I even looked at your code I thought this might be happening.

If you''re calling AddBullet too often, then all the bullets in the array will become active, but the distance between each will be negligable, so it will look like only 1 bullet is being fired. You should also add a refire time for each bullet, and after 1 bullet is fired, don''t be able to shoot another until the time has passed.

Just a thought...

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Yeah, just like BeerNutts said. Try using the following principle,

if (player.reload > 0)
player.reload--;

if ((fire key is pressed) && (player.reload == 0))
{
doFireCode();
}

within your code that [successfully] fires a bullet,

player.reload = 25;

The bit of code that decrements the player''s reload counter will have to be within the mainloop so it is decrementing each frame.

That''s just a rough piece of code, but it should give you the idea.

Project: Starfighter
http://www.parallelrealities.co.uk
Parallel RealitiesProject: Starfighter
Ok I did that, and it works, but when the player requests a new bullet and the old one is still flying visibly, it removes it and the new one shoots, which is annoying. Also, after the first bullet, the next ones speed up, until eventually they are blazing around at light speed. I''m not sure whats causing the problem. I keep the VelocityX and VelocityY of the bullets (actually, all entities) as floats, and update their positions like so: LocalXPosition += VelocityX; LocalXPosition is fed to the destination RECT. Is the conversion from float to int somehow causing strange errors?
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content

This topic is closed to new replies.

Advertisement