Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help with Projectiles  (Read 6731 times)

0 Members and 1 Guest are viewing this topic.

Pacman

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Help with Projectiles
« 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;
}

 

« Last Edit: December 03, 2014, 07:05:48 am by Laurent »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with Projectiles
« Reply #1 on: December 03, 2014, 02:54:20 am »
What is i?
« Last Edit: December 03, 2014, 07:05:54 am by Laurent »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Pacman

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Help with Projectiles
« Reply #2 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?
« Last Edit: December 03, 2014, 07:06:01 am by Laurent »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Help with Projectiles
« Reply #3 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...

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Help with Projectiles
« Reply #4 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.

Pacman

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Help with Projectiles
« Reply #5 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 ?

Pacman

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Help with Projectiles
« Reply #6 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?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help with Projectiles
« Reply #7 on: December 03, 2014, 08:12:19 pm »
Isn't it obvious?
Draw all the bullets at their respective positions each frame.

Pacman

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Help with Projectiles
« Reply #8 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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Help with Projectiles
« Reply #9 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.

Pacman

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Help with Projectiles
« Reply #10 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  :)

Pacman

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Help with Projectiles
« Reply #11 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 ?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Help with Projectiles
« Reply #12 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.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Help with Projectiles
« Reply #13 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. ;)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Help with Projectiles
« Reply #14 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/