SFML и C++ Уроки \ Разработка игр › Форумы › SFML System, Window и другие вопросы › Ошибка при инициализации переменной в классе
В этой теме 2 ответа, 2 участника, последнее обновление Igor97 8 года/лет, 1 месяц назад.
-
АвторСообщения
-
C++12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576#include<iostream>#include<SFML/Graphics.hpp>using namespace sf;class Player{public:float x, y, w, h, dx, dy;float speed = 0;int dir = 0;String File;Image image;Texture texture;Sprite sprite;Player (String F, int X, int Y, float W, float H){File = F;w = W, h = H;image.loadFromFile("Images/" + File);image.createMaskFromColor(Color(41, 33, 59));texture.loadFromImage(image);sprite.setTexture(texture);x = X; y = Y;sprite.setTextureRect(IntRect(w,h,w,h));}void update(float time){switch(dir){case 0: dx = speed; dy = 0; break;case 1: dx = -speed; dy = 0; break;case 2: dx = 0; dy = speed; break;case 3: dx = 0; dy =-speed; break;}x += dx*time;y += dy*time;speed = 0;sprite.setPosition(x,y);}};int main(){RenderWindow window(sf::VideoMode(640, 480), "Lesson 4");Clock clock;Player p("hero.png",250,250,96.0,96.0);float CurrentFrame = 0;while (window.isOpen()){sf::Event event;while(window.pollEvent(event)){if (event.type==sf::Event::Closed)window.close();}float time = clock.getElapsedTime().asMicroseconds();clock.restart();time = time/800;if ((Keyboard::isKeyPressed(Keyboard::Left)||(Keyboard::isKeyPressed(Keyboard::A)))) {p.dir = 1; p.speed = 0.1; CurrentFrame += 0.005*time; if(CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame),96,96,96));}if ((Keyboard::isKeyPressed(Keyboard::Right)||(Keyboard::isKeyPressed(Keyboard::D)))) {p.dir = 0; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame),192,96,96));}if ((Keyboard::isKeyPressed(Keyboard::Up)||(Keyboard::isKeyPressed(Keyboard::W)))) {p.dir = 3; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame),288,96,96));}if ((Keyboard::isKeyPressed(Keyboard::Down)||(Keyboard::isKeyPressed(Keyboard::S)))) {p.dir = 2; p.speed = 0.1; CurrentFrame += 0.005*time; if (CurrentFrame > 3) CurrentFrame -= 3; p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame),0,96,96));}if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {p.sprite.setColor(Color::Red);}if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {p.sprite.setColor(Color::White);}p.update(time);window.clear();window.draw(p.sprite);window.display();}return 0;}
здесь ошибка float speed = 0;
int dir = 0; использование инициализатора элемента данных не допускаетсяВложения:
You must be logged in to view attached files.надо мне поправить форум, откуда то “quot;” берется в твоем коде при вставке сюда =\
Теперь по твоей проблеме – скорее всего ошибка в том, что твой компилятор по старее моего и в нем еще не была реализована такая вещь как инициализация переменной в классе. Поэтому инициализируй переменные в конструкторе класса. То есть оставь как есть
float speed;
int dir;А в конструкторе уже пиши
Player(String F, int X, int Y, float W, float H){
dir = 0; speed = 0;Тему перенес, в другой раздел
Спасибо за помощь!
-
АвторСообщения
Для ответа в этой теме необходимо авторизоваться.