本文介绍了从sfml中的矢量删除Sprite 2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从矢量中删除精灵,但没有效果,我不知道是什么问题(VS 2010与SFML 2.1)。它根本不编译。任何提示赞赏。

I'm trying to remove sprites from a vector, but to no avail and I don't know what's wrong(VS 2010 with SFML 2.1). It simply doesn't compile. Any tips appreciated.

这行,似乎有问题:

SpriteVector。 erase(std :: remove(SpriteVector.begin(),SpriteVector.end(),SpriteIt),SpriteVector.end());

完整代码:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    sf::RenderWindow mMainWindow(sf::VideoMode(1200, 900), "Map");
    mMainWindow.setFramerateLimit(60);

    sf::RectangleShape rect;
    rect.setSize(sf::Vector2f(1500, 500));
    sf::Texture unittexture;
    unittexture.loadFromFile("sprite.png");

    std::vector<sf::Sprite> SpriteVector;
    std::vector<sf::Sprite>::iterator SpriteIt;

    while (mMainWindow.isOpen())
    {
        sf::Event event;
        while (mMainWindow.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                mMainWindow.close();
            }
            else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::A)   
            {
                sf::Sprite newSprite;
                newSprite.setTexture(unittexture);
                newSprite.setPosition(mMainWindow.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(mMainWindow).x-50,sf::Mouse::getPosition(mMainWindow).y-50))); 
                SpriteVector.push_back(newSprite);
            }
            else if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Right)
                    {
                    //  sf::Vector2f mousecoords(mMainWindow.mapPixelToCoords(sf::Vector2i(event.mouseButton.x, event.mouseButton.y)));     
                    //  SpriteVector.remove_if([=](sf::Sprite newSprite){return newSprite.getGlobalBounds().contains(mousecoords); });     //example code that would work with a list

                        SpriteVector.erase(std::remove(SpriteVector.begin(), SpriteVector.end(), SpriteIt), SpriteVector.end());           // This doesn't work and I don't know why.
                    }
        }   
    mMainWindow.clear();
    mMainWindow.draw(rect);

        for(auto &SpriteIt = SpriteVector.begin();SpriteIt != SpriteVector.end();++SpriteIt)
        {
            mMainWindow.draw(*SpriteIt);
        }
        mMainWindow.display();


    }

    return 0;
}

错误日志:

bei Anpassung der Argumentliste '(sf::Sprite, const std::_Vector_iterator<_Myvec>)'
1>          with
1>          [
1>              _Myvec=std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1829): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_FwdIt std::_Remove<sf::Sprite*,_Ty>(_FwdIt,_FwdIt,const _Ty &)".
1>          with
1>          [
1>              _FwdIt=sf::Sprite *,
1>              _Ty=std::_Vector_iterator<std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>>
1>          ]
1>          c:\users\bogdan\documents\visual studio 2010\projects\spielkarte bev\sfmltest\main.cpp(40): Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template "_FwdIt std::remove<std::_Vector_iterator<_Myvec>,std::_Vector_iterator<_Myvec>>(_FwdIt,_FwdIt,const _Ty &)".
1>          with
1>          [
1>              _FwdIt=std::_Vector_iterator<std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>>,
1>              _Myvec=std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>,
1>              _Ty=std::_Vector_iterator<std::_Vector_val<sf::Sprite,std::allocator<sf::Sprite>>>
1>          ]


推荐答案

$ c> SpriteIt 作为 remove 的最后一个参数,而不是要从容器中删除的值。

You're passing a type, SpriteIt, as the last argument to remove instead of the value you want removed from the container.

这篇关于从sfml中的矢量删除Sprite 2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:55