I'm new in using SFML and I've just done algorithm for movement of my character in my game. Everything works on my computer but I'm not sure is it a good way of doing animation with SFML. It looks like that
sf::Time time= timer.getElapsedTime();
if(time < t1) { s = 0; sprajt.setTexture(leftleg); }
if (time > t1 && time < t2){ if (s == 0){ sprajt.move(30,0); s = 1; } sprajt.setTexture(stand); }
if (time > t2 && time < t3) { s = 0; sprajt.setTexture(rightleg); }
if (time > t3 && time < t4){ if ( s== 0){ sprajt.move(30,0); s = 1; } sprajt.setTexture(stand); }
if (time > t4) timer.restart();
}
Firsteval it is changing texture to "left leg texture", after t1 it is moved by 30 pixels and texture is changed to "standing texture", etc.
I'm curious is there (in SFML) better way of doing it? For example with framerate?
In case you use Thor, you could also take a look at the Animation module (http://www.bromeon.ch/libraries/thor/v2.0/doc/group___animation.html).
You can use it as follows (see here (https://github.com/Bromeon/Thor/blob/master/examples/Animation.cpp) for a more complex example):
// Create animation, where each frame has specified duration and texture rect
thor::FrameAnimation animation;
animation.addFrame(sf::seconds(0.2f), sf::IntRect(...));
animation.addFrame(sf::seconds(0.1f), sf::IntRect(...));
// Create object that stores multiple animations and accesses them by string
thor::Animator<sf::Sprite, std::string> animator;
animator.addAnimation("anim", animation, sf::seconds(1.f));
animator.playAnimation("anim");
// Game loop: Update animator according to passed time, animate sprite
sf::Time frameTime = ...;
animator.update(frameTime);
animator.animate(sprite);
The animations are not limited to texture rects, also colorizing and fading animations are provided. And you can define your own animations easily.