SFML community forums

Help => Graphics => Topic started by: Pacman on December 03, 2014, 02:23:07 am

Title: Help with Projectiles
Post by: Pacman on December 03, 2014, 02:23:07 am
Hey everyone. I am doing a space game
Here is the code of the main function.
When i press space only one bullet is fired and it keeps reappearing when i press space.
can anyone tell me why does this happen.

i want as many bullets that i fire to appear on the screen. please help 

int main()
{
int i=0;

Ship shipObject;
Bullet bullletObject;
vector <Bullet> bulletArsenal;


   
    myGameGUI.create(sf::VideoMode(1000,680), "WorkingGameProject");///Creates the Window
    sf::Style::Default;

    Background();///Creates the background
    shipObject.createShip(shipTexture, shipImage);///calls function ship class and creates the ship

    while(myGameGUI.isOpen())
    {
     while(myGameGUI.pollEvent(action))
        {
        closeFunction();
        }
        bool fire =false;

shipObject.moveShip(action, shipImage,myGameGUI);///Calls function from ship class and Moves the ship

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
        bulletArsenal.push_back(bullletObject);/// adds a new object to the vector
        bulletArsenal[i].createBullet(bulletTexture,bulletImage,shipImage);/// creates a bullet of a specific object
        fire=true;
        }

        bulletArsenal[i].moveBullet(bulletImage); ///move the bullet of a specific object


        myGameGUI.clear();///clears screen
        myGameGUI.draw(backgroundImage);///draws the background
        shipObject.drawShip(myGameGUI,shipImage);///draws the ship
        bulletArsenal[i].drawBullet(myGameGUI,bulletImage);///draws the bullet of a specific object
        myGameGUI.display();

        if(fire==true)
        i++;
    }
return 0;
}

 

Title: Re: Help with Projectiles
Post by: Hapax on December 03, 2014, 02:54:20 am
What is i?
Title: Re: Help with Projectiles
Post by: Pacman on December 03, 2014, 04:40:01 am
What is i?

i is the index for the vector .
Why do you think the the bullet keeps reappearing and multiple bullets wont appear on the screen?
Title: Re: Help with Projectiles
Post by: Ixrec on December 03, 2014, 08:46:39 am
It might have something to do with the fact that you're only drawing one of the bullets every frame.

Also, I swear I've seen this code and given this exact response in a previous thread...
Title: Re: Help with Projectiles
Post by: Gambit on December 03, 2014, 09:46:16 am
Also, I swear I've seen this code and given this exact response in a previous thread...

http://en.sfml-dev.org/forums/index.php?topic=16835
http://en.sfml-dev.org/forums/index.php?topic=16866

He also posted about 5 other topics with the same title and description but they seem to have been removed as I cant find them and my post history does not show them despite me posting in them.
Title: Re: Help with Projectiles
Post by: Pacman on December 03, 2014, 08:09:24 pm
It might have something to do with the fact that you're only drawing one of the bullets every frame.

Ok thanks Ixrec for pointing out the problem, how should fix this problem ?
Title: Re: Help with Projectiles
Post by: Pacman on December 03, 2014, 08:12:07 pm


He also posted about 5 other topics with the same title and description but they seem to have been removed as I cant find them and my post history does not show them despite me posting in them.

Yes Gambit i did make some post to i could increase the number of eyes that saw it. Thanks for responding to my other posts.
What did suggest on the other posts?
Title: Re: Help with Projectiles
Post by: Jesper Juhl on December 03, 2014, 08:12:19 pm
Isn't it obvious?
Draw all the bullets at their respective positions each frame.
Title: Re: Help with Projectiles
Post by: Pacman on December 03, 2014, 08:50:30 pm

Draw all the bullets at their respective positions each frame.

I am not to sure how to do it .
Jesper can you please show me an example of that?
Title: Re: Help with Projectiles
Post by: G. on December 03, 2014, 09:12:52 pm
Whenever you want to do something with all the objects in a container, you want to iterate over that container.  Do you know what "iterating over a container" actually means?  It feels like you might be lacking a fundamental understanding of C++, which is necessary to use a library like SFML effectively.
Title: Re: Help with Projectiles
Post by: Pacman on December 03, 2014, 09:43:44 pm
 
Do you know what "iterating over a container" actually means?  It feels like you might be lacking a fundamental understanding of C++, which is necessary to use a library like SFML effectively.

Ok thanks G. i know that i am not a strong coder. Just learning as i go along  :)
Title: Re: Help with Projectiles
Post by: Pacman on December 03, 2014, 09:47:33 pm
Whenever you want to do something with all the objects in a container, you want to iterate over that container.

Hey G
I am iterating over the container with the while loop, using int i as the index number... Take a look at it .
Is that not how its done ?
Title: Re: Help with Projectiles
Post by: Ixrec on December 03, 2014, 10:56:03 pm
In all seriousness, you need to read a good book on the C++ language.  How to work with containers and how while/for loops work are basic things you absolutely must know before you do anything useful with C++ at all, much less a library written in C++ like SFML.

https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list is a good starting point.
Title: Re: Help with Projectiles
Post by: G. on December 03, 2014, 11:04:34 pm
Obviously not, you're drawing only one bullet whereas you want to draw every bullets every frame.
Do you know for loops? (if not, you really should) Your vector contains bulletArsenal.size() bullets. If you want to draw them all you'll have to draw your bullets from bulletArsenal[0] to bulletArsenal[size-1] every frame. You can use a for loop in order to do that. (there are several other ways to iterate over a vector, you can document yourself about iterators or range based for loops)
for (int i = 0; i < bulletArsenal.size(); ++i) {
    bulletArsenal[i].drawBullet(myGameGUI,bulletImage);
}

And like Ixrec said, using SFML requires C++ knowledge, learning both at the same time looks like a bad idea, or you'll be stuck every 5 minutes on very basic things and create the same topic a few times. ;)
Title: Re: Help with Projectiles
Post by: eXpl0it3r on December 03, 2014, 11:05:19 pm
Yes Gambit i did make some post to i could increase the number of eyes that saw it.
Don't open new threads on the same topic. If you keep doing this, your threads will simply get deleted.
Title: Re: Help with Projectiles
Post by: Pacman on December 04, 2014, 12:37:11 am
Don't open new threads on the same topic. If you keep doing this, your threads will simply get deleted.

Ok thanks exploter .Laurent messaged me and made everything clear