sprite animation mit SMFL
-
hi könnt ihr mir paar tipps geben wie ich eine flüssige sprite animation hinkriege?
meine jetzige sieht nicht so flüssig aus
Animation.h
#ifndef ANIMATION_H #define ANIMATION_H #include <SFML/Graphics.hpp> #include <QList> #define LEFT 0 #define RIGHT 1 #define JUMP 2 #define STAND 3 struct T_ANIMATION { int moveFrames; int jumpFrames; int standFrames; int width; int height; float xPos; float speed; }; class Animation { public: Animation(T_ANIMATION para, sf::Image *image); void move(float x, int way); sf::Sprite getSprite(){return m_sprite;} private: void init(); sf::RenderWindow *m_pApp; sf::Sprite m_sprite; QList<sf::IntRect> m_moveRight; QList<sf::IntRect> m_moveLeft; QList<sf::IntRect> m_jump; QList<sf::IntRect> m_stand; T_ANIMATION m_para; int m_way; int m_frame; }; #endif // ANIMATION_H
Animation.cpp
#include "Animation.h" Animation::Animation(T_ANIMATION para, sf::Image *image) { m_para = para; m_frame = 0; sf::IntRect rect; rect.Left = 0; rect.Right = 30; rect.Top = 0; rect.Bottom = 60; init(); m_sprite.SetImage(*image); m_sprite.SetSubRect(rect); } void addIntoList(QList<sf::IntRect> *list, int left, int right, int top, int bottom) { sf::IntRect rect; rect.Left = left; rect.Right = right; rect.Top = top; rect.Bottom = bottom; list->append(rect); } void Animation::init() { // moveRight for(int i = 1;i < (m_para.moveFrames+1);i++) addIntoList(&m_moveRight, i * m_para.width, (i+1) * m_para.width, 0, m_para.height ); // moveLeft for(int i = 1;i < (m_para.moveFrames+1);i++) addIntoList(&m_moveLeft, i * m_para.width, (i+1) * m_para.width, m_para.height, m_para.height * 2 ); } void Animation::move(float x, int way) { if(way == RIGHT) { m_frame++; m_sprite.SetSubRect(m_moveRight.at(m_frame)); m_sprite.Move(x, 0); } else { m_frame++; m_sprite.SetSubRect(m_moveLeft.at(m_frame)); m_sprite.Move(x*(-1), 0); } if(m_frame >= (m_para.moveFrames-1)) m_frame = 0; }
main.cpp
#include <SFML/Graphics.hpp> #include "Animation.h" int main() { // Create the main rendering window sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML"); sf::Image img; if(!img.LoadFromFile("player.png")) return -1; T_ANIMATION para; para.moveFrames = 5; para.height = 60; para.width = 30; Animation player( para, &img); // Start game loop while (App.IsOpened()) { // Process events sf::Event Event; while (App.GetEvent(Event)) { // Close window : exit if (Event.Type == sf::Event::Closed) App.Close(); } float ElapsedTime = App.GetFrameTime(); // Move the sprite if (App.GetInput().IsKeyDown(sf::Key::Right)) player.move(200 * ElapsedTime, RIGHT); if (App.GetInput().IsKeyDown(sf::Key::Left)) player.move(200 * ElapsedTime, LEFT); sf::Sleep(0.05f); App.Clear(sf::Color(255,255,255)); App.Draw(player.getSprite()); App.Display(); } return EXIT_SUCCESS; }
hab für die animation ein png verwendet
-
hat keiner einen tipp für mich wie mans macht?
das problem is das ich nach jedem tastendruck das frame um 1 position verschiebe und dann 50 ms warte bis der nächste tastendruck ausgelsöt werden kann und dann verschiebe ich wieder um 1 frame weiter bei 6 setz ich wieder auf 0 usw
-
v = s/t
-
helplezz schrieb:
hi könnt ihr mir paar tipps geben wie ich eine flüssige sprite animation hinkriege?
[über 150 Zeilen Code]
Wenn du Hilfe erwartest, poste einen minimalen und vollständigen Code, der dein Problem verdeutlicht. Also alles Irrelevante rausstreichen, aber das Wichtige behalten, sodass der Code immer noch verständlich ist und im Zusammenhang mit deiner Fragestellung steht.