SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Зацикливается анимация
Помечено: control sfml animation
В этой теме 2 ответа, 3 участника, последнее обновление Heisenberg 8 года/лет, 5 мес. назад.
Просмотр 3 сообщений - с 1 по 3 (из 3 всего)
-
АвторСообщения
-
Зацикливается анимация после отжимания клавиш. Помогите пожалуйста.
C++12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970#include <SFML/Graphics.hpp>using namespace sf;class Player{public: float x, y, w, h, dx, dy, speed, CurrentFrame;String File;Image image;Texture texture;Sprite sprite;enum { left, right, up, down, downdright, downdleft, updright, updleft, stay } state;Player(String F, int X, int Y, float W, float H){state = stay;CurrentFrame = 0;dx = 0; dy = 0;y = 0; x = 0;w = W; h = H;x = X; y = Y;File = F; speed = 0;image.loadFromFile("Images/" + File);texture.loadFromImage(image);sprite.setTexture(texture);sprite.setTextureRect(IntRect(0, 0, w, h));}void control(){if (Keyboard::isKeyPressed(Keyboard::Left)) { state = left; speed = 0.1; }if (Keyboard::isKeyPressed(Keyboard::Right)) { state = right; speed = 0.1; }if (Keyboard::isKeyPressed(Keyboard::Up)) { state = up; speed = 0.1; }if (Keyboard::isKeyPressed(Keyboard::Down)) { state = down; speed = 0.1; }if ((Keyboard::isKeyPressed(Keyboard::Right)) && (Keyboard::isKeyPressed(Keyboard::Down))) { state = downdright; speed = 0.1; }if ((Keyboard::isKeyPressed(Keyboard::Left)) && (Keyboard::isKeyPressed(Keyboard::Down))) { state = downdleft; speed = 0.1; }if ((Keyboard::isKeyPressed(Keyboard::Right)) && (Keyboard::isKeyPressed(Keyboard::Up))) { state = updright; speed = 0.1; }if ((Keyboard::isKeyPressed(Keyboard::Left)) && (Keyboard::isKeyPressed(Keyboard::Up))) { state = updleft; speed = 0.1; }}void animation(float time){if (state == right) { CurrentFrame += 0.005*time; if (CurrentFrame > 5) CurrentFrame -= 5; sprite.setTextureRect(IntRect(18 * int(CurrentFrame), 84, 18, 28)); }if (state == left) { CurrentFrame += 0.005*time; if (CurrentFrame > 5) CurrentFrame -= 5; sprite.setTextureRect(IntRect(18 * int(CurrentFrame), 56, 18, 28)); }if (state == up) { CurrentFrame += 0.005*time; if (CurrentFrame > 5) CurrentFrame -= 5; sprite.setTextureRect(IntRect(20 * int(CurrentFrame), 28, 20, 28)); }if (state == down) { CurrentFrame += 0.005*time; if (CurrentFrame > 5) CurrentFrame -= 5; sprite.setTextureRect(IntRect(20 * int(CurrentFrame), 0, 20, 28)); }}void update(float time){animation(time);control();switch (state){case right: dx = speed; dy = 0; break;case left: dx = -speed; dy = 0; break;case down: dx = 0; dy = speed; break;case up: dx = 0; dy = -speed; break;case downdright: dx = speed; dy = speed; break;case downdleft: dx = -speed; dy = speed; break;case updright: dx = speed; dy = -speed; break;case updleft: dx = -speed; dy = -speed; break;}x += dx*time;y += dy*time;speed = 0;sprite.setPosition(x, y);}};попробуй вывести state значение переменной , может оно резко меняется в каком то случае
Нужно добавить что-то вроде
C++1if(Keyboard::isKeyReleased) state = null;В данном случае придётся заменить enum {} state на переменные типа bool
т.к. enum может содержать в себе всего одно состояние, а bool можно включать или выключать по надобности. -
АвторСообщения
Просмотр 3 сообщений - с 1 по 3 (из 3 всего)
Для ответа в этой теме необходимо авторизоваться.