【发布时间】:2016-10-18 01:02:03
【问题描述】:
我正在使用 sfml 用 C++ 开发一个小游戏。当您按下 TAB 按钮时,屏幕右侧会出现一个带有四个按钮的小菜单。我创建了一个class Button,在课堂上我有一个sf::Text 属性。这是我用来绘制菜单的代码。
void DrawRightSideMenu(RenderWindow &win)
{
window.draw(rectInGameMenu);
for (list<Button>::iterator currentButton = rightSideMenuButtons.begin(); currentButton != rightSideMenuButtons.end(); currentButton++)
{
win.draw(*currentButton);
Text buttonTextToDraw = currentButton->GetButtonText(); //Crash here
win.draw(buttonTextToDraw);
}
}
在执行此代码之前,所有按钮都在另一个函数中创建。 rectInGameMenu 只是一个矩形,它工作正常。 win.draw(*currentButton); 运行良好,但按钮中没有预期的文本。所以当我使用GetButtonText() 时程序崩溃了。这样我就可以在按钮中绘制文本。我收到一条错误消息,指出它无法读取内存中的那个位置(Violation d'accès lors de la Lecture de l'emplacement 0x550030BD)。对不起法语,我用法语的 Visual Studio。我真的不知道我能做些什么来解决这个问题。我做了一些研究,但找不到任何有同样问题的人......我已经有几天这个问题了,我真的不知道我能做些什么来解决它。如果有更好的方法,请随时告诉我。
编辑:
这是我创建按钮的代码:
Button aButton;
aButton = Button(10, "Ressources/Fonts/font.ttf");
aButton.SetButtonText("Button Text");
aButton.setSize(Vector2f(246, 60));
aButton.setFillColor(Color(22, 235, 65));
aButton.setPosition(773, 120);
aButton.GetButtonText().setPosition(240, 30);
rightSideMenuButtons.push_back(aButton);
这是在创建游戏窗口时调用的 void 函数中。现在这是我在class Button 中使用的方法:
Button::Button(int inFontSize, string inFontButton)
{
fontSize = inFontSize;
fontButton.loadFromFile(inFontButton);
}
void Button::SetButtonText(string inButtonText)
{
buttonText.setString(inButtonText);
buttonText.setFont(fontButton);
buttonText.setCharacterSize(fontSize);
buttonText.setColor(Color(255, 255, 255));
}
这些是函数使用的方法。 buttonText 是 Text 属性。 fontButton 是 Font 属性。希望对大家有所帮助。
【问题讨论】:
标签: c++ visual-studio sfml