如何使用 C++ 在 Visual Studio 2017 中修复此 textureBackground 标识符

How do I fix this textureBackground identifier in Visual Studio 2017 using C++

我在尝试声明标识符时遇到了问题。主要部分是textureBackground.loadFromFile("graphics/background.png"); 其中 textureBackground 是带下划线的

我试过添加括号、更改大写、小写、检查文件位置等

int main()
{
    //Create a video mode object
    VideoMode vm(1920, 1080);

    // Create and open a window for game
    RenderWindow window(vm, "Scarful!!!", Style::Fullscreen);
    while (window.isOpen())

        // Texture for graphic on cpu
        Texture textureBackground;

        // Load graphic into texture
         textureBackground.loadFromFile("graphics/background.png");

        // Make Sprite
        Sprite spriteBackground;

        // Attach texture to sprite
        spriteBackground.setTexture(textureBackground);

        // Set spritebackground to cover screen
        spriteBackground.setPosition(0, 0);
    {
        /* Handle player input */

        if (Keyboard::isKeyPressed(Keyboard::Escape))
        {
            window.close();

        }

        //Update Scene


        //Draw Scene
        window.clear();
        //Draw Game Scene
        window.draw(spriteBackground);

        //Show everything we drew
        window.display();
    }
    return 0;
}

这里,

while (window.isOpen())
    // Texture for graphic on cpu
    Texture textureBackground;
// Load graphic into texture
textureBackground.loadFromFile("graphics/background.png");

您正在尝试这样做:

while (window.isOpen()) {
    // Variable goes out of scope outside of the loop...
    Texture textureBackground;
}
   textureBackground.loadFromFile("graphics/background.png");
// ^^^^^^^^^^^^^^^^^ is not available anymore...

并且由于 textureBackground 超出范围,您不能再修改它...我建议您想要...

// Texture for graphic on cpu
Texture textureBackground;

// Load graphic into texture
textureBackground.loadFromFile("graphics/background.png");

// Make Sprite
Sprite spriteBackground;

// Attach texture to sprite
spriteBackground.setTexture(textureBackground);

// Set spritebackground to cover screen
spriteBackground.setPosition(0, 0);

while (window.isOpen()) {
    // Other code goes here...
}