【问题标题】:SFML 2.1 Full Explanation of using Vectors to create multiple SpritesSFML 2.1 使用向量创建多个精灵的完整解释
【发布时间】:2014-04-11 14:43:09
【问题描述】:

我在代码块中使用 SFML 2.1,但我不知道如何使用向量来克隆我的小行星精灵。它一直说 asteroid_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;
    }

【问题讨论】:

  • 缩进是可读性​​的关键。
  • 你的主循环在几个方面有问题(whiles),请查看网站sfml-dev.org/tutorials/2.1上的教程。

标签: c++ vector sprite sfml


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多