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

Author Topic: Help needed:Delete element of vecotor (erase)  (Read 1396 times)

0 Members and 1 Guest are viewing this topic.

Dannehood

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Help needed:Delete element of vecotor (erase)
« on: April 23, 2012, 01:50:03 pm »
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

Code: [Select]
#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

Code: [Select]
#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();
}
}

« Last Edit: April 23, 2012, 03:19:12 pm by Dannehood »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10827
    • View Profile
    • development blog
    • Email
Re: Help needed:Delete element of vecotor (erase)
« Reply #1 on: April 23, 2012, 05:31:37 pm »
I'm too lazy to go through all of your sourcecode but
if ( count > 0 && myBullet[1].sprite.GetPosition().x < -1)
{
        count--;
        bulletlist.erase(bulletlist.begin());
        // ...
}

If you want to remove the first element (bulletlist.begin()) why do you check wether the second element (myBullet[1].) is 'offscreen'?
Keep in mind arrays and vectors start in C++ with the index 0 not with 1.

Also learn how to use your debugger, you can easily look at the states where something happens that shouldn't happen and figure out why this or that variable was set wrong.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/