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;
}
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. ;)