SFML 2.1使用矢量创建多个精灵的完整说明

SFML 2.1 Full Explanation of using Vectors to create multiple Sprites

本文关键字:精灵 说明 创建 SFML      更新时间:2023-10-16

我在代码块中使用SFML 2.1,我不知道如何使用Vectors来克隆我的小行星精灵。它一直说,尚未声明星号_V,并弹出一个警告框,称其"在所选编码中使用非法字符",并且"更改这些字符是为了保护[我]不丢失数据"。

该程序的目标是连续创建小行星精灵,这些精灵将在屏幕上方的随机点生成,然后直接下降。程序中还有其他精灵和方面,但我从这篇文章中删除了它们,以适当地浓缩它。这似乎是唯一的问题。

int n;
int main()
{
    RenderWindow window;
    window.setFramerateLimit(30);
    RenderWindow mainMenu;
    srand( time(0));
    Texture asteroid_Pic;
    asteroid_Pic.loadFromFile("Asteroid.png");
    std::vector<sf::Sprite> asteroid(n, Sprite(asteroid_Pic));
    for (int i = 0; i < asteroid.size(); i++){
        asteroid[n].setOrigin(15, 15);
        asteroid[n].getPosition();
        asteroid[n].setPosition(x = rand() % 790 + 10, y = rand() % -10 - 50);
    }
    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == Event::Closed){
                window.close();
            }
            asteroid[n].setPosition(x, y+=1);
            asteroid[n].rotate(1);
            // clear the window with black color
            window.clear(Color::Black);
            // draw everything here...
            // window.draw(...);
            window.draw(player1);
            window.draw(asteroid[n]);
            // end the current frame
            window.display();
        }
        return 0;
    }

您的主循环中有另一个while (window.isOpen())。你的程序将进入主循环,然后永远不会离开那个内部循环。它永远不会至少画一次。

您需要摆脱内部的while (window.isOpen())循环,并找到另一种方法。

虽然最初的问题是关于计时器的,但你可以在这里找到游戏循环的基本解释。如果你想根据时间做一些事情(移动精灵,创建新的ingame实体),你必须在循环中处理时间。