Archived

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

Bullets acting up

This topic is 5652 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

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]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites