SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Проблемы с анимацией
В этой теме 2 ответа, 2 участника, последнее обновление gelius 3 года/лет, 11 мес. назад.
Просмотр 3 сообщений - с 1 по 3 (из 3 всего)
-
АвторСообщения
-
Запуская игру, получаю вот это http://s019.radikal.ru/i627/1605/2c/7aea9b55afea.png
Не могу понять, почему так происходит, точнее, почему кадры не убираются…
Нарезал с помощью SpriteDecomposer
Animation_HPP
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147#ifndef ANIMATION_H#define ANIMATION_H#include "TinyXML/tinyxml.h"#include <SFML/Graphics.hpp>using namespace sf;class Animation{public:std::vector<IntRect> frames, frames_flip;float currentFrame, speed;bool loop, flip, isPlaying;Sprite sprite;Animation(){currentFrame = 0;isPlaying=true;flip=false;loop=true;}void tick(float time){if (!isPlaying) return;currentFrame += speed * time;if (currentFrame > frames.size()) { currentFrame -= frames.size();if (!loop) {isPlaying=false; return;}}int i = currentFrame;sprite.setTextureRect( frames[i] );if (flip) sprite.setTextureRect( frames_flip[i] );}};class AnimationManager{public:std::string currentAnim;std::map<std::string, Animation> animList;AnimationManager(){}~AnimationManager(){ animList.clear();}void create(std::string name, Texture &texture, int x, int y, int w, int h, int count, float speed, int step=0, bool Loop=true){Animation a;a.speed = speed;a.loop = Loop;a.sprite.setTexture(texture);a.sprite.setOrigin(0,h);for (int i=0;i<count;i++){a.frames.push_back( IntRect(x+i*step, y, w, h) );a.frames_flip.push_back( IntRect(x+i*step+w, y, -w, h) );}animList[name] = a;currentAnim = name;}void loadFromXML(std::string fileName,Texture &t){TiXmlDocument animFile(fileName.c_str());animFile.LoadFile();TiXmlElement *head;head = animFile.FirstChildElement("sprites");TiXmlElement *animElement;animElement = head->FirstChildElement("animation");while(animElement){Animation anim;currentAnim = animElement->Attribute("title");int delay = atoi(animElement->Attribute("delay"));anim.speed = 1.0/delay; anim.sprite.setTexture(t);TiXmlElement *cut;cut = animElement->FirstChildElement("cut");while (cut){int x = atoi(cut->Attribute("x"));int y = atoi(cut->Attribute("y"));int w = atoi(cut->Attribute("w"));int h = atoi(cut->Attribute("h"));anim.frames.push_back( IntRect(x,y,w,h) );anim.frames_flip.push_back( IntRect(x+w,y,-w,h) );cut = cut->NextSiblingElement("cut");}anim.sprite.setOrigin(0,anim.frames[0].height);animList[currentAnim] = anim;animElement = animElement->NextSiblingElement("animation");}}void set(std::string name){currentAnim = name;animList[currentAnim].flip=0;}void draw(RenderWindow &window,int x=0, int y=0){animList[currentAnim].sprite.setPosition(x,y);window.draw( animList[currentAnim].sprite );}void flip(bool b=1) {animList[currentAnim].flip = b;}void tick(float time) {animList[currentAnim].tick(time);}void pause() {animList[currentAnim].isPlaying=false;}void play() {animList[currentAnim].isPlaying=true;}void play(std::string name) {animList[name].isPlaying=true;}bool isPlaying() {return animList[currentAnim].isPlaying;}float getH() {return animList[currentAnim].frames[0].height;}float getW() {return animList[currentAnim].frames[0].width;}};#endif ANIMATION_HPPGAME.HPP
C++1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768void RunGame(){Music music;music.openFromFile("resources/sounds/VJLINK_MUSIC_1.ogg");music.play();music.setVolume(50);RenderWindow window(VideoMode(1080, 500), "VJLINK GAME");View view(FloatRect(0,0,1080,500));Level lvl;lvl.LoadFromFile("map1.tmx");Texture vjlink_t, enemy_t;vjlink_t.loadFromFile("resources/images/VJLINKSET.png");enemy_t.loadFromFile("resources/images/VJLINKSET.png");AnimationManager anim;anim.loadFromXML("resources/anim_vjlink.xml",vjlink_t);anim.animList["jump"].loop = 0;AnimationManager anim2;anim2.create("move",enemy_t,0,0,16,16,2,0.002,18);anim2.create("dead",enemy_t,48,0,16,16,1,0);Object pl = lvl.GetObject("player");Object enemy =lvl.GetObject("enemy");PLAYER Mario(anim, lvl, pl.rect.left, pl.rect.top);ENEMY enemy1(anim2,lvl,enemy.rect.left,enemy.rect.top);Clock clock;while (window.isOpen()){float time = clock.getElapsedTime().asMicroseconds();clock.restart();time = time/500;if (time > 40) time = 40;Event event;while (window.pollEvent(event)){if (event.type == Event::Closed)window.close();}Mario.update(time);enemy1.update(time);if (Keyboard::isKeyPressed(Keyboard::Left)) Mario.key["L"]=true;if (Keyboard::isKeyPressed(Keyboard::Right)) Mario.key["R"]=true;if (Keyboard::isKeyPressed(Keyboard::Up)) Mario.key["Up"]=true;lvl.Draw(window);enemy1.draw(window);Mario.draw(window);window.display();}}#endif // GAME_HPPPLAYER.HPP
C++123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104#ifndef PLAYER_HPP#define PLAYER_HPP#include "Entity.hpp"class PLAYER :public Entity{public:enum { stay,walk,duck,jump,climb,swim } STATE;std::map<std::string,bool> key;PLAYER(AnimationManager &a, Level &lev,int x,int y):Entity(a,x,y){option("Player",0, 1 ,"stay");STATE=stay;obj = lev.GetAllObjects();}void Keyboard(){if (key["L"]){dir=1;if (STATE!=duck) dx=-0.1;if (STATE==stay) STATE=walk;}if (key["R"]){dir=0;if (STATE!=duck) dx=0.1;if (STATE==stay) STATE=walk;}if (key["Up"]){if (STATE==stay || STATE==walk) { dy=-0.27; STATE=jump; anim.play("jump");}}/////////////////////если клавиша отпущена///////////////////////////if (!(key["R"] || key["L"])){dx=0;if (STATE==walk) STATE=stay;}if (!(key["Up"])){if (STATE==climb) dy=0;if (STATE=jump) dy=0; STATE=stay;}key["R"]=key["L"]=key["Up"]=false;}void Animation(float time){if (STATE==stay) anim.set("stay");if (STATE==walk) anim.set("walk");if (STATE==jump) anim.set("jump");if (STATE==duck) anim.set("duck");if (dir) anim.flip();anim.tick(time);}void update(float time){Keyboard();Animation(time);x += dx*time;Collusion(0);y += dy*time;Collusion(1);}void Collusion(int num){for (int i=0;i<obj.size();i++)if (getRect().intersects(obj[i].rect)){if (obj[i].name=="solid"){if (dy>0 && num==1) { y = obj[i].rect.top - h; dy=0; STATE=stay;}if (dy<0 && num==1) { y = obj[i].rect.top + obj[i].rect.height ; dy=0;}if (dx>0 && num==0) { x = obj[i].rect.left - w; }if (dx<0 && num==0) { x = obj[i].rect.left + obj[i].rect.width ;}}}}};#endif // PLAYER_HPPЕсли требуется весь проект https://yadi.sk/d/_dNy0orHrSUDP
Заранее спасибо!
Использование window.clear решило проблему
Как его написать в данном случае?
Подскажи, пожалуйста -
АвторСообщения
Просмотр 3 сообщений - с 1 по 3 (из 3 всего)
Для ответа в этой теме необходимо авторизоваться.