Jump to content
  • Advertisement
Sign in to follow this  
FantasyVI

SFML Bullet problem

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

hay guys

ok so i have made a bullet array and when i click the left button on the mouse the bullet get the player position and move from there, but the problem is whenever i click on the mouse it doesn't generate a new Bullet but it just remove the old one and get the player position again and move from there.

can you guys please help? thankx



#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Zombie Game Alpha");

sf::Image PlayerImage, BulletImage;
sf::Sprite PlayerSprite, BulletSprite[10];

PlayerImage.LoadFromFile("player.png");
BulletImage.LoadFromFile("bullet.png");

PlayerSprite.SetImage(PlayerImage);

for(int x=0; x<=9; x++)
BulletSprite[x].SetImage(BulletImage);

PlayerSprite.SetPosition(200,200);

bool Draw=false;
int Counter=0;

while(Window.IsOpened())
{
sf::Event Event;

float ElapsedTime = Window.GetFrameTime();

if (Window.GetInput().IsKeyDown(sf::Key::W)) PlayerSprite.Move(0, -425 * ElapsedTime);
if (Window.GetInput().IsKeyDown(sf::Key::S)) PlayerSprite.Move(0, 425 * ElapsedTime);
if (Window.GetInput().IsKeyDown(sf::Key::A)) PlayerSprite.Move(-425 * ElapsedTime, 0);
if (Window.GetInput().IsKeyDown(sf::Key::D)) PlayerSprite.Move(425 * ElapsedTime, 0);

while (Window.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Window.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Window.Close();

if(Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
Draw=true;
Counter++;
for(int x=0; x<=Counter; x++)
BulletSprite[x].SetPosition(PlayerSprite.GetPosition().x,PlayerSprite.GetPosition().y);
}

}

Window.Clear();
Window.Draw(PlayerSprite);

if(Draw==true)
{
for(int x=0; x<=Counter; x++)
{
BulletSprite[x].Move(200 * ElapsedTime, 0);
Window.Draw(BulletSprite[x]);
}
std::cout << Counter;
}
Window.Display();
}
}

Share this post


Link to post
Share on other sites
Advertisement

hay guys

ok so i have made a bullet array and when i click the left button on the mouse the bullet get the player position and move from there, but the problem is whenever i click on the mouse it doesn't generate a new Bullet but it just remove the old one and get the player position again and move from there.

can you guys please help? thankx




if(Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
Draw=true;
Counter++;
for(int x=0; x<=Counter; x++)
BulletSprite[x].SetPosition(PlayerSprite.GetPosition().x,PlayerSprite.GetPosition().y);
}




Are you really sure you want a for loop right here? You're incrementing counter to point to the next position in the array, but then you iterate the array to set the positions of all the bullets previous to the current bullet, as well as the current bullet.

Share this post


Link to post
Share on other sites

[quote name='FantasyVI' timestamp='1311514215' post='4839588']
hay guys

ok so i have made a bullet array and when i click the left button on the mouse the bullet get the player position and move from there, but the problem is whenever i click on the mouse it doesn't generate a new Bullet but it just remove the old one and get the player position again and move from there.

can you guys please help? thankx




if(Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
Draw=true;
Counter++;
for(int x=0; x<=Counter; x++)
BulletSprite[x].SetPosition(PlayerSprite.GetPosition().x,PlayerSprite.GetPosition().y);
}




Are you really sure you want a for loop right here? You're incrementing counter to point to the next position in the array, but then you iterate the array to set the positions of all the bullets previous to the current bullet, as well as the current bullet.
[/quote]
i just can figer out how to code it, i spent 2 hours trying and nothing came up.
i tried

int BulletX[10];
int BulletY[10];
for(int x=0; x<=Counter; x++)
{
BulletX[x] = PlayerSprite.getPosition().x;
BulletY[x] = PlayerSprite.getPosition().x;
BulletSprite[x].SetPosition(BulletX[x],BulletY[x]);
}


but its not working, i tried lots of stuff but nothing working.

could you give me a little code that i can work with or just tell me what should i code ?
thankx

Share this post


Link to post
Share on other sites

i just can figer out how to code it, i spent 2 hours trying and nothing came up.
i tried

int BulletX[10];
int BulletY[10];
for(int x=0; x<=Counter; x++)
{
BulletX[x] = PlayerSprite.getPosition().x;
BulletY[x] = PlayerSprite.getPosition().x;
BulletSprite[x].SetPosition(BulletX[x],BulletY[x]);
}


but its not working, i tried lots of stuff but nothing working.

could you give me a little code that i can work with or just tell me what should i code ?
thankx


Yet again, I have to ask: do you really want to use a for loop there? Think for a minute what the for-loop is doing. It is looping through all the bullets up to Counter (including the bullets previous to Counter, which were bullets already created earlier) and setting their position to the player's position. In essence, you are taking every bullet you have already created, and putting them all into one big pile directly on top of the player.

Specifically, try:

BulletSprite[Counter].SetPosition(PlayerSprite.getPosition().x, PlayerSprite.getPosition().y)

and see what happens.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!