SFML community forums

Help => General => Topic started by: grimmreefer on March 18, 2013, 09:01:09 pm

Title: Is it possible to erase a vector element while push_back
Post by: grimmreefer on March 18, 2013, 09:01:09 pm
Hy Guys,

while pressing the left mousebutton i push back a few elements in  a vector<struck>. Every element has a lifetime, if the lifetime is over
a function delete the element with erase(), but only when i release the mousebutton, so if no new objects are added.

So, how can i push_back and erase at same time?
Title: Re: Is it possible to erase a vector element while push_back
Post by: Foaly on March 18, 2013, 10:48:40 pm
Use insert instead of push_back.
Title: Re: Is it possible to erase a vector element while push_back
Post by: Nexus on March 18, 2013, 11:03:26 pm
Please wait until you get a meaningful answer here (http://www.c-plusplus.de/forum/314929).

You won't get better answers by asking the same question in different forums, as long as your problem description is not useful.
Title: Re: Is it possible to erase a vector element while push_back
Post by: grimmreefer on March 18, 2013, 11:04:41 pm
No, did not work

Here i at the elements:

    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        sf::Vector2f temp;
        temp.x = window.mapPixelToCoords(sf::Mouse::getPosition(window)).x;
        temp.y = window.mapPixelToCoords(sf::Mouse::getPosition(window)).y;

        _PAR._ADDPARTICLE("SPARK", 10, 20, 0, "Images/SPARK.png", temp, 20, 0);
    }


if(_Data._Type == "SPARK")
{
    for(unsigned int i = 0; i < counter; i++)
    {
        unsigned int _Direction = rand()%360;
        _Data._Direction = _Direction;
        _Data._Sprite.setRotation(_Direction);


        _AllParticle.insert(_AllParticle.begin(),_Data);
    }
}

at the same time this funktion checks the lifetime:

    if(_Timer.getElapsedTime().asMilliseconds() >= 100)
        {
            _Timer.restart();

            for(unsigned int o = 0; o < _AllParticle.size(); ++o)
            {
                static float value = _AllParticle[o]._LifeTime/2.f;

                _AllParticle[o]._LifeTime -= 0.1f;

                if(_AllParticle[o]._LifeTime  <= value)
                {


                    _AllParticle[o]._Alpha -= _AllParticle[o]._Alpha / _AllParticle[o]._LifeTime;
                    _AllParticle[o]._Sprite.setColor(sf::Color(255,255,255, _AllParticle[o]._Alpha));

                }
            }

        }


But booth things does not work at the same time, if i add something the check function is waiting until i release the mousebutton.