Sign in to follow this  

SFML Bullet problem

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

[code]

#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();
}
}
[/code]

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

[code]


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);
}
[/code]

[/quote]

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='JTippetts' timestamp='1311516349' post='4839591']
[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

[code]


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);
}
[/code]

[/quote]

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
[code]
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]);
}
[/code]

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
[quote name='FantasyVI' timestamp='1311519478' post='4839604']
i just can figer out how to code it, i spent 2 hours trying and nothing came up.
i tried
[code]
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]);
}
[/code]

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
[/quote]

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:
[code]
BulletSprite[Counter].SetPosition(PlayerSprite.getPosition().x, PlayerSprite.getPosition().y)
[/code]
and see what happens.

Share this post


Link to post
Share on other sites

This topic is 2334 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this