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

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

我在尝试声明标识符时遇到了问题。主要部分是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;
}

c++ visual-c++ sfml
1个回答
1
投票

这里,

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...
}
© www.soinside.com 2019 - 2024. All rights reserved.