I am creating bullets that move to the left, when first bullet have passed a certain point I want to delete it to make room for a new bullet to take it's place. I've put comments where the problem lies.
Declarations.h
#ifndef DECLARATIONS_H
#define DECLARATIONS_H
class bullet
{
public:
bullet(){shoot = true;}
sf::Clock clock;
sf::Sprite sprite;
void setShoot(bool setShoot){shoot = setShoot;}
bool getShoot(){return shoot;}
private:
bool shoot;
};
#endif
main.cpp
#include <iostream>
#include <vector>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"
using namespace std;
int main ()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Bullet");
App.SetFramerateLimit(90);
sf::Image Bullet_Bill;
Bullet_Bill.LoadFromFile("Bullet_Bill.png");
int count = 0;
bullet bulletMember;
vector <bullet> bulletlist;
vector <bullet> myBullet(10);
vector <bullet>::iterator start = bulletlist.begin();
vector <bullet>::iterator start2 = myBullet.begin();
while(App.IsOpened())
{
sf::Event Event;
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
if (bulletMember.clock.GetElapsedTime() > 1.0 && bulletMember.getShoot() == true && count < 9)
{
count ++;
cout << "Bang! " << count << endl;
bulletlist.push_back(myBullet[count]);
myBullet[count].sprite.SetImage(Bullet_Bill);
myBullet[count].sprite.SetPosition(App.GetWidth()- myBullet[count].sprite.GetSize().x, 0);
bulletMember.setShoot(false);
}
if (bulletMember.getShoot() == false)
{
bulletMember.clock.Reset();
bulletMember.setShoot(true);
}
//-----------need help with this bit----------------
if ( count > 0 && myBullet[1].sprite.GetPosition().x < -1)
{
count--;
bulletlist.erase(bulletlist.begin());
//when the first bullet is touching the left edge all bullets gets deleted but I only want 1 bullet to get deleted and
// when the next bullet is touching it I want that one to get deleted and so on.
//cout<<myBullet[1].sprite.GetPosition().x << endl;
}
//--------------------------------------------------
App.Clear(sf::Color(255,255,255,255));
for(int i = 0; i <= count; i ++)
{
myBullet[i].sprite.Move(-150 * App.GetFrameTime(), 0);
App.Draw(myBullet[i].sprite);
}
App.Display();
}
}