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

Author Topic: Extra sprite spawning when the enemies are redrawn SFML  (Read 2415 times)

0 Members and 1 Guest are viewing this topic.

PapaSmurf

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Extra sprite spawning when the enemies are redrawn SFML
« on: March 23, 2015, 01:38:05 am »
I am making a game in sfml and at the moment when all of the enemies die. They are set to respawn however when this is happening they are respawning with one extra sprite than before.

The code for loading in the sprites is
Code: [Select]
unsigned int orcNumber = 5;
for (int i = 0; i < orcNumber; i++)
{
    SpriteVector.push_back(ogreSprite);
    SpriteVector[i].setPosition(spawnPointX[i], spawnPointY[i]);
}
The code for removing the enemies if they are offscreen or shot is similar to below using erase.

for (unsigned j = 0; j < SpriteVector.size(); j++)
{
    if (this->SpriteVector[j].getPosition().x < 0 - 80 )
    {
        //this succesfully removes the object from the vector
        SpriteVector.erase(SpriteVector.begin() + j);
        std::cout << "Container size..." << SpriteVector.size() << "\n";
    }
}
The statement for redrawing them is:

unsigned int orcNumberRespawn = 5;
if (SpriteVector.size() <= 1)
{           
    for (int i = 0; i < orcNumberRespawn; i++)
    {
        SpriteVector.push_back(ogreSprite);
        SpriteVector[i].setPosition(spawnPointX[i], spawnPointY[i]);
    }
}

window.draw(SpriteVector[i]);
Can anyone identify why when the sprites need to be redrawn it draws with + 1 sprite everytime?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Extra sprite spawning when the enemies are redrawn SFML
« Reply #1 on: March 23, 2015, 07:52:46 am »
You only erase the sprite if the x position is smaller than -80. Now if one has a bigger x position it won't get removed and your push_back will simply add more elements.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Extra sprite spawning when the enemies are redrawn SFML
« Reply #2 on: March 23, 2015, 09:29:54 am »
BTW, there's another flaw in your for loop: when removing one element you should not increase j otherwise you will skip an element.
SFML / OS X developer

PapaSmurf

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Extra sprite spawning when the enemies are redrawn SFML
« Reply #3 on: March 23, 2015, 09:41:28 am »
Could you advise how to write the loop without skipping the elements?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Extra sprite spawning when the enemies are redrawn SFML
« Reply #4 on: March 23, 2015, 09:43:30 am »
In pseudo code:

For (i = 0; i <= size; /*nothing*/)
Do
  if (isCandidateForRemoval(array[i])) remove ith element
  else ++i
End
 
« Last Edit: March 23, 2015, 09:46:43 am by Hiura »
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Extra sprite spawning when the enemies are redrawn SFML
« Reply #5 on: March 23, 2015, 09:53:38 am »
Never call std::vector::erase() in an iteration loop, it's terribly inefficient. Use std::remove_if() instead.
And iterate with iterators or the range-based for loop, not with indices.

See also:
http://en.sfml-dev.org/forums/index.php?topic=5555.msg36407#msg36407
http://en.sfml-dev.org/forums/index.php?topic=10557.msg73133#msg73133
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything