如何在 c++/sfml 2.1 中使任何精灵消失

How to make any sprite dissapear in c++/sfml 2.1?

本文关键字:任何 精灵 消失 c++ sfml      更新时间:2023-10-16

我是一个初学者,正在开发一个用c ++/sfml的游戏。我不知道如何在任何特定条件下使任何形状或精灵消失?
例如:-

if(A-KEY-IS-PRESSED)
{
    sprite.disappear
}

我想现在每个人都能理解我在说什么。如何在 sfml/c++ 中做到这一点?

在你的代码中的某个地方,你有一个RenderWindow,我们称之为win。你正在做这样的事情:

win.draw(sprite);

要让精灵消失,就不要那样做。

if(A-KEY-IS-PRESSED)
    sprite_is_visible = false;
...
if (sprite_is_visible)
    win.draw(sprite);

如果您已经知道如何创建精灵(从列表中),这对您来说可能是一个很好的解决方案。

如果没有,那么只需告诉我,我会给你一个完全有效的代码示例。

去除精灵的代码:

            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)));
                    EnemyList.remove_if([=](sf::Sprite newSprite){return newSprite.getGlobalBounds().contains(mousecoords); });
                }