title: Selecting Sprites, that were created "ingame"
body: <p>I've have been searching the web thoroughly for several days now and could't come up with any solution for the following problem: It's about C++ and SFML 2.1. I would like being able to select "ingame"-generated single Sprites by middle(button)-clicking on them (and later doing something with them). If there are more than one sprite at any given position (overlapping), than I would like to select the topmost one. I'm using a list and whenever I try to select any Sprite, only the one, that has been created as last, "reacts" correctly (that is printig a console text). Many thanks in advance for any help.</p>
  
  <p>Full code:</p>
  
  <pre><code>#include &lt;SFML/Graphics.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;list&gt;
  
  int main()
  {
      sf::RectangleShape rectangle;
      rectangle.setSize(sf::Vector2f(1000, 600));
      rectangle.setFillColor(sf::Color(100,80,100,100));
      rectangle.setPosition(0, 0);
  
      sf::Sprite newSprite;
  
      sf::FloatRect rightBound = rectangle.getGlobalBounds();
      sf::FloatRect unitrect = newSprite.getGlobalBounds();
  
      sf::RenderWindow mMainWindow(sf::VideoMode(1000, 600), "Map");
      mMainWindow.setFramerateLimit(60);
      sf::Texture unittexture;
      unittexture.loadFromFile("warrior.png");
  
      std::list&lt;sf::Sprite&gt; EnemyList;
      std::list&lt;sf::Sprite&gt;::iterator EnemyIt;
  
      while (mMainWindow.isOpen())
      {
          sf::Event event;
  
          while (mMainWindow.pollEvent(event))
          {
              if (event.type == sf::Event::Closed)
              {
                  mMainWindow.close();
              }
              if (event.type == sf::Event::MouseButtonPressed &amp;&amp; event.mouseButton.button == sf::Mouse::Middle)
                  if(newSprite.getGlobalBounds().contains(mMainWindow.mapPixelToCoords(sf::Mouse::getPosition(mMainWindow))))
                      std::cout &lt;&lt; "Unit selected" &lt;&lt; std::endl;
  
              if(event.type == sf::Event::MouseButtonPressed)
                  if(event.mouseButton.button == sf::Mouse::Right)
                  {
                      sf::Vector2f mousecoords(mMainWindow.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)));
                      EnemyList.remove_if([=](sf::Sprite unitsprite){return unitsprite.getGlobalBounds().contains(mousecoords); });
                  }
              if(event.type == sf::Event::MouseButtonPressed)
                  if(event.mouseButton.button == sf::Mouse::Left)
                      if (rectangle.getGlobalBounds().contains(mMainWindow.mapPixelToCoords(sf::Mouse::getPosition(mMainWindow))))        
                      {
                          newSprite.setTexture(unittexture);
                          newSprite.setPosition(sf::Mouse::getPosition(mMainWindow).x,sf::Mouse::getPosition(mMainWindow).y);
                          EnemyList.push_back(newSprite);
                      }
          }
  
          for(EnemyIt = EnemyList.begin();EnemyIt != EnemyList.end();EnemyIt++)
          {
              mMainWindow.draw(*EnemyIt);
          }
          mMainWindow.display();
          mMainWindow.clear(sf::Color(0, 200, 0, 255));
      }
  
      return 0;
  }
  </code></pre>