【发布时间】:2023-03-26 13:17:01
【问题描述】:
我正在尝试使用 SFML 创建一些包含纹理和精灵的类。 当我在我的类的 SFML 库中创建类对象时 - 我不能在 main.xml 中使用该对象。 我想知道如何在 main 中显示精灵(例如):
class MainMenu
{
public:
int DrawMenu(){
sf::Texture texture;
if (!texture.loadFromFile("idle.png"))
return EXIT_FAILURE;
sf::Sprite spritemenu(texture);
return 0;
}
};
int main()
{
// Create the main window
sf::RenderWindow app(sf::VideoMode(800, 600), "SFML window");
// Load a sprite to display
sf::Texture texture;
if (!texture.loadFromFile("cb.bmp"))
return EXIT_FAILURE;
sf::Sprite sprite(texture);
MainMenu menu;
// Start the game loop
while (app.isOpen())
{
// Process events
sf::Event event;
while (app.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
app.close();
}
// Clear screen
app.clear();
// Draw the sprite
app.draw(sprite);
menu.DrawMenu();
app.draw(spritemenu);
// Update the window
app.display();
}
return EXIT_SUCCESS;
}
【问题讨论】:
-
请记住,当 DrawMenu 完成时,局部变量会超出范围。也许你想要类成员变量而不是局部变量(所以它们在 DrawMenu 完成后存在)?
-
是的,当然。如何让它工作?