窗口位置将不会更改sfml c ++

问题描述 投票:-1回答:1

我现在的目标是将窗口重新定位到桌面上的随机位置。似乎很基本,我可能只是犯了一个愚蠢的错误,但是没有抛出任何错误,尽管该窗口没有重新定位。我正在尝试使用一个将返回随机vector2i的类,但是当我使用该窗口时,它不会重定位。

我定义Key不是错误,但很容易知道

    typedef struct KeySprite {
        sf::Image Img;
        sf::Texture Tex;
        sf::Sprite Sprite;
    }Key;

The Random Vector2i

    static sf::Vector2i RandSpawn(sf::Image image) 
    {
        int RandX = (rand() % sf::VideoMode::getDesktopMode().width) + image.getSize().x + 1;
        int RandY = (rand() % sf::VideoMode::getDesktopMode().height) + image.getSize().y + 1;
        cout << RandX << " and " << RandY << endl;
        //I put the cout statement to test if it ever actually ran this. It doesn't apear to be
        return sf::Vector2i(RandX, RandY);
    }
    static void DrawKey(string key)
    {
        const unsigned char opacity = 1000;
        Key Key;
        sf::RenderWindow window(sf::VideoMode(Key.Img.getSize().x, Key.Img.getSize().y, 32), "Key", sf::Style::None);
        setTransparency(window.getSystemHandle(), opacity);
        if (key == "A") 
        Key.Img.loadFromFile("Assets/Images/A.png");
        else if (key == "D") 
        Key.Img.loadFromFile("Assets/Images/D.png");
        else if (key == "E") 
            Key.Img.loadFromFile("Assets/Images/E.png");
        else if (key == "Q")
            Key.Img.loadFromFile("Assets/Images/Q.png");
        else if (key == "S") 
            Key.Img.loadFromFile("Assets/Images/S.png");
        else if (key == "W") 
            Key.Img.loadFromFile("Assets/Images/W.png");
        else if (key == "X") 
            Key.Img.loadFromFile("Assets/Images/X.png");
        else if (key == "Z") 
            Key.Img.loadFromFile("Assets/Images/Z.png");
        else if (key == "Esc") 
            Key.Img.loadFromFile("Assets/Images/esc.png");
        setShape(window.getSystemHandle(), Key.Img);
        Key.Tex.loadFromImage(Key.Img);
        Key.Sprite.setTexture(Key.Tex);
        window.setPosition(RandSpawn(Key.Img));
        while (window.isOpen())
        {
            // Process events
            sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed)
                    window.close();
            }
            window.clear(sf::Color::Transparent);
            window.draw(Key.Sprite);
            window.display();
        }
    }

};

还引用了一些其他变量以使窗口稍微透明,但这并不重要。我也从主要根据所按下的键来称呼它。非常感谢您的帮助,如果它是一个愚蠢的错误。

c++ c++17 sfml
1个回答
0
投票

我不知道我做了什么更改,但现在可以正常工作>>

© www.soinside.com 2019 - 2024. All rights reserved.