SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Class Text › Ответ в теме: Class Text
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class setText { public: Font font; setText(String sText, String sFont, int iSize,int iColor,float x, float y) { font.loadFromFile("fonts/" + sFont); Text text1(sText, font, iSize); text1.setStyle(Text::Bold); switch (iColor) { case 1: text1.setFillColor(Color::Red); case 2: text1.setFillColor(Color::Green); case 3: text1.setFillColor(Color::Blue); case 4: text1.setFillColor(Color::Yellow); case 5: text1.setFillColor(Color::Black); default: text1.setFillColor(Color::White); } text1.setPosition(x, y); } }; |
Ну а собственно, как ты собрался тут обращатся к text1 если в конце конструктора класса будет автоматически вызван его деструктор так как он окажется вне поля видимости всей программы? Если ты хочешь просто использовать конструктор текста достаточно обьявить статический метод. Так же желательно Font так же обьявить статическим (Или держать его обьявление в начале int mani() дабы не терять его из поля видимости на протяжении всего жизненого цикла программы)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
static Font publicFont //В int Mmain() просто добавляем publicFont.loadFromFile("путь"); static Text setText(String sText, int iSize,int iColor,float x, float y) { Text text1(sText, publicFont, iSize); text1.setStyle(Text::Bold); switch (iColor) { case 1: text1.setFillColor(Color::Red); case 2: text1.setFillColor(Color::Green); case 3: text1.setFillColor(Color::Blue); case 4: text1.setFillColor(Color::Yellow); case 5: text1.setFillColor(Color::Black); default: text1.setFillColor(Color::White); } text1.setPosition(x, y); return text1; } ///Что бы получить доступ к методу в любом место просто после объявления инклудов дописываешь extern static Text setText(String sText, String sFont, int iSize,int iColor,float x, float y) |