Jump to content
  • Advertisement
Sign in to follow this  
LeftyGuitar

Can't get Bullet to shoot [SFML/C++]

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

Hello,

I can't get a bullet to shoot. When I hit the key, nothing happens. I've posted my code. It compiles fine, but nothing happens when I press the space key, which is supposed to fire the bullet.

sf::RenderWindow GameWin;
sf::Event event;

struct PLAYER
{
	sf::RectangleShape shape;
	sf::Vector2f Pos;

	float X, Y;
};

struct BULLET
{
	sf::CircleShape shape;
	sf::Vector2f Pos, Vel;

	float X, Y;
	float Speed;
	bool Visible;
};

PLAYER player;
BULLET bullet;

int main(int argc, char* argv[])
{
	GameWin.create(sf::VideoMode(800, 600, 32), "");

	player.X = 800 / 2;
	player.Y = 600 / 2;
	player.Pos = sf::Vector2f(player.X, player.Y);
	player.shape.setPosition(sf::Vector2f(player.Pos));
	player.shape.setSize(sf::Vector2f(30, 30));
	player.shape.setFillColor(sf::Color(0, 255, 0));

	bullet.X = player.X;
	bullet.Y = player.Y;
	bullet.Visible = false;
	bullet.Speed = 0.0f;
	bullet.Pos = sf::Vector2f(bullet.X, bullet.Y);
	bullet.shape.setPosition(sf::Vector2f(bullet.Pos));
	bullet.shape.setScale(sf::Vector2f(10, 10));
	bullet.shape.setFillColor(sf::Color(255, 0, 0));

	while (GameWin.isOpen())
	{
		while (GameWin.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
			{
				GameWin.close();
			}

			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
			{
				bullet.Visible = true;
				bullet.Speed = 10.0f;
			}
		}

		GameWin.clear();

		GameWin.draw(player.shape);

		if (bullet.Visible)
		{
			GameWin.draw(bullet.shape);
			bullet.shape.move(bullet.Speed, 0);

			if (bullet.X >= 800)
			{
				bullet.Visible = false;
			}
		}

		GameWin.display();
	}

	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement

Have you tried setting breakpoints? 2 good spots would be inside the if statement that sets the bullet.Visible to true, and inside the if statement that checks if bullet.Visible.

 

Set breakpoints, run the code, inspect what the variables are when the breakpoints are hit, and then step through line by line and see if something seems wrong somewhere.

 

It does seem a bit suspicious that a BULLET has a Vector2f Pos, 2x floats for X and Y, and then a shape which also seems to contains something called Position.

 

EDIT: Also, haven't you done this several times now? This seemed super familiar to me, and my memory agreed with your post history:

https://www.gamedev.net/topic/687437-bullet-doesent-fire-correctly/ -- this is even with the same framework.

https://www.gamedev.net/topic/679121-cant-get-bullets-to-shoot/

Edited by Lactose

Share this post


Link to post
Share on other sites

OK I figured out the first part of my problem. I had setScale, what I needed was setRadius in order for the bullet shape to appear. However when I run the debugger on the if visible statement, it says it is still false, despite pressing the space key, which is supposed to set it to true.

 

EDIT: Ok I got it. I set the value to true in the debugger and it fixed it. Now I can fire the bullet and it shows when I hit the space key. It didn't seem like there was a bug in the code, but guess there was. Now to work on making it so I can fire multiple bullets.

	bullet.X = player.X;
	bullet.Y = player.Y;
	bullet.Visible = false;
	bullet.Speed = 0.0f;
	bullet.shape.setPosition(sf::Vector2f(bullet.X,bullet.Y));
	bullet.shape.setRadius(10);
	bullet.shape.setFillColor(sf::Color(255, 0, 0));
Edited by LeftyGuitar

Share this post


Link to post
Share on other sites

OK, so now I am trying to add more bullets, or make it where you can fire more than one bullet. However whenever I hit the space key, it makes a bullet appear one after the other. It doesn't move to the right of the screen like it did, when there was just one bullet.

 

EDIT: I figured it out. I had to change this line of code

bullet.shape.move(bullets[i].Speed, 0);

//Needed to be this
bullets[i].shape.move(bullets[i].Speed,0);
vector <BULLET> bullets;

bullet.X = player.X;
	bullet.Y = player.Y;
	bullet.Visible = false;
	bullet.Speed = 0.0f;
	bullet.shape.setPosition(sf::Vector2f(bullet.X,bullet.Y ));
	bullet.shape.setRadius(10);
	bullet.shape.setFillColor(sf::Color(255, 0, 0));

while (GameWin.isOpen())
	{
		while (GameWin.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
			{
				GameWin.close();
			}

			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
			{
				bullet.Visible = true;
				bullet.Speed = 0.010f;

				bullets.push_back(bullet);
			}
  }

		GameWin.clear();

		GameWin.draw(player.shape);

		if (bullet.Visible)
		{
		
			for (int i = 0; i < bullets.size(); i++)
			{

				GameWin.draw(bullets[i].shape);
				bullet.shape.move(bullets[i].Speed, 0);

				if (bullets[i].shape.getPosition().x >= 800)
				{
					bullets[i].Visible = false;
					bullets[i].Speed = 0.0f;
				}
			
			}
		}

		GameWin.display();
	}
Edited by LeftyGuitar

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!