应用程序崩溃并同时运行

The application crashes and runs at the same time

我画的是应用菜单,之前写过菜单class和这个class的功能。

当我启动应用程序时,菜单会工作 5-7 秒,但在 Windows 中,光标以蓝色圆圈的形式旋转,这不是什么好兆头。如果这5-7秒是空闲的,整个屏幕 变成白色,并且 Windows 表示应用程序没有响应。在这种情况下,如果您按下按钮损坏的位置,这些按钮就会被触发,菜单关闭,游戏开始。问题出在菜单的描述上class,我想。

菜单代码class:

class Menu
{
public:
    Image menuImage1, menuImage2, menuImage3;
    Texture menuTexture1, menuTexture2, menuTexture3;

    int menuNum = 0;
    bool isMenu;

    list <Asteroid*> asteroidsMass;
    list <Asteroid*> ::iterator it;

    Sprite menu1, menu2, menu3;

    Menu()
    {
        menuImage1.createMaskFromColor(Color::Black);
        menuImage2.createMaskFromColor(Color::Black);
        menuImage3.createMaskFromColor(Color::Black);

        menuImage1.loadFromFile("title.jpg");
        menuImage2.loadFromFile("start.jpg");
        menuImage3.loadFromFile("exit.jpg");

        menuTexture1.loadFromImage(menuImage1);
        menuTexture2.loadFromImage(menuImage2);
        menuTexture3.loadFromImage(menuImage3);

        menu1.setTexture(menuTexture1);
        menu2.setTexture(menuTexture2);
        menu3.setTexture(menuTexture3);

        menu1.setPosition(scrX / 2, 200);
        menu2.setPosition((scrX / 2) - 172.5, 300);
        menu3.setPosition((scrX / 2) - 140, 450);

        menu1.setOrigin(300, 75);

        for (int i = 0; i < 15; i++)
        {
            Asteroid* aster = new Asteroid(rand() % 1200, rand() % 800, 5, 25, 0);
            asteroidsMass.push_back(aster);
        }
        isMenu = true;
    }

    void update(RenderWindow& app)
    {
        while (isMenu)
        {
            menu2.setColor(Color::White);
            menu3.setColor(Color::White);

            app.clear(Color::Black);

            menuNum = 0;

            if (IntRect((scrX / 2) - 172, 300, 360, 100).contains(Mouse::getPosition(app))) { menu2.setColor(Color::Green); menuNum = 2; }
            else if (IntRect((scrX / 2) - 172, 450, 360, 100).contains(Mouse::getPosition(app))) { menu3.setColor(Color::Green);menuNum = 3; }

            if (Mouse::isButtonPressed(Mouse::Left))
            {

                if (menuNum == 2) 
                {
                    for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
                    {
                        Asteroid* asteroid = *it;
                        it = asteroidsMass.erase(it);
                        delete asteroid;
                    }
                    isMenu = false;
                
                }
                if (menuNum == 3) 
                { 
                    for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
                    {
                        Asteroid* asteroid = *it;
                        it = asteroidsMass.erase(it);
                        delete asteroid;
                    }
                    
                    isMenu = false; 
                    app.close(); 
                    
                }

            }

            for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
            {
                app.draw((*it)->EntityShape);
            }

            for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
            {
                (*it)->update();
            }

            app.draw(menu1);
            app.draw(menu2);
            app.draw(menu3);

            app.display();
        }
    }
};

全部代码: https://pastebin.pl/view/0c1d6c9e

我解决了我的问题! 如果你有同样的问题,只需写在“while”循环检查结束 window:

while(isMenu)
{
Event event;
    while (app.pollEvent(event))
        {
            if (event.type == Event::Closed)
                app.close();
        }
//some code
}