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

Author Topic: Is it possible to erase a vector element while push_back  (Read 2502 times)

0 Members and 1 Guest are viewing this topic.

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Is it possible to erase a vector element while push_back
« 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?

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Is it possible to erase a vector element while push_back
« Reply #1 on: March 18, 2013, 10:48:40 pm »
Use insert instead of push_back.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Is it possible to erase a vector element while push_back
« Reply #2 on: March 18, 2013, 11:03:26 pm »
Please wait until you get a meaningful answer here.

You won't get better answers by asking the same question in different forums, as long as your problem description is not useful.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

grimmreefer

  • Jr. Member
  • **
  • Posts: 91
    • View Profile
    • Email
Re: Is it possible to erase a vector element while push_back
« Reply #3 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.