I've been fiddling with this for hour trying to figure out why the last frame doesn't show.By setting the bool repeating to false I found that it sort of skips the fourth frame and counts the first as the fourth.Any ideas on why this is happening?The part that does the animation is the function update().
class SPR
{
public:
Sprite sprite;
bool isanimation;
Clock clock0;
IntRect textureRect;
IntRect currentRect;
int frames;
int remainingframes;
bool nextrow=false;
Texture referencetexture;
bool repeats;
int customXreturnpoint;//the position at which the animation resets to the next row;
float delay;
startanimation(IntRect startrect,int pframes,float p_delay=0.15,bool p_repeats=false,int p_customXreturnpoint=0)
{
if(!isanimation)
{
frames=pframes;
remainingframes=frames;
repeats=p_repeats;
nextrow=false;
textureRect=startrect;
currentRect=textureRect;
customXreturnpoint=p_customXreturnpoint;
delay=p_delay;
isanimation=true;
}
}
SPR(RenderWindow &window,Texture &texture,IntRect ptextureRect=IntRect(0,0,0,0),Vector2f pixelpos=Vector2f(0,0),bool animated=false,int pframes=0,float p_delay=0.15,bool p_repeats=false,int p_customXreturnpoint=0)
{
if(ptextureRect==IntRect(0,0,0,0))
{
ptextureRect=IntRect(0,0,texture.getSize().x,texture.getSize().y);
}
if(pframes==0)//if user types 0 then change it to 1 since 0 just removes first frame if this is ever used as animation.Even if the startfunction sets the frames it still will cut off the first frame if the declaration function sets frames as 0.
{
cout<<"Warning! One of your animations is set to frame 0 at declaration."<<endl<<" Only positive numbers exist for frames!"<<endl<<" Frame 0 has been changed to 1 to accomodate."<<endl;
pframes=1;
}
isanimation=animated;
sprite.setTexture(texture);
sprite.setTextureRect(ptextureRect);
textureRect=ptextureRect;
frames=pframes;
repeats=p_repeats;
remainingframes=frames;
sprite.setPosition(pixelpos);
referencetexture=texture;
currentRect=textureRect;
customXreturnpoint=p_customXreturnpoint;
delay=p_delay;
}
update(RenderWindow &w);
};
SPR::update(RenderWindow &w)
{
if(customXreturnpoint==0)
{
customXreturnpoint=referencetexture.getSize().x;
}
if(isanimation)//The last frame is getting skipped and should not!
{
if(clock0.getElapsedTime().asSeconds()>delay)
{
if(remainingframes>0)
{
if(currentRect.left<=customXreturnpoint-currentRect.width)
{
remainingframes--;
currentRect.left+=currentRect.width;
}
if(currentRect.left>=customXreturnpoint-currentRect.width)
{
currentRect.top+=currentRect.height;
currentRect.left=textureRect.left;
remainingframes--;
}
}
if(remainingframes==0)
{
if(repeats==true)
{
currentRect=textureRect;
remainingframes=frames;
}
else
{
currentRect=textureRect;
remainingframes=frames;
isanimation=false;
}
}
sprite.setTextureRect(currentRect);
clock0.restart();
}
}
w.draw(sprite);
}
int main()
{
RenderWindow window(VideoMode(640,480),"Animation");
Texture t1,t2,t3,t4,t5;
t1.loadFromFile("ff-stat.png");
t2.loadFromFile("hiker.png");
t3.loadFromFile("Mario.png");
t4.loadFromFile("birdfly.png");
t5.loadFromFile("squirrel.png");
SPR test1(window,t1,IntRect(0,0,128,128),Vector2f(300,300),true,13,0.15,true);
SPR test2(window,t3,IntRect(0,0,16,33),Vector2f(400,300),true,48,0.15,true);
SPR test3(window,t1,IntRect(0,0,128,128),Vector2f(300,200),false,13,0.15,true);
SPR test4(window,t2,IntRect(0,0,50,50),Vector2f(300,100),false,4,0.5,true);
SPR test5(window,t1,IntRect(0,0,128,128),Vector2f(500,300),true,13,0.15,true);
SPR test6(window,t2,IntRect(0,0,50,50),Vector2f(600,300),true,4,0.15,true);
SPR test7(window,t4,IntRect(0,0,195,195),Vector2f(400,200),true,4,0.5,false);
SPR test8(window,t5,IntRect(6,25,158,105),Vector2f(200,200),true,8,0.15,true);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(Color::Yellow);
if(Keyboard::isKeyPressed(Keyboard::Left))
{
test2.startanimation(IntRect(0,0,16,33),42,0.15);test2.sprite.move(-0.3,0);
}
if(Keyboard::isKeyPressed(Keyboard::Right)) test2.sprite.move(0.3,0);
if(Keyboard::isKeyPressed(Keyboard::Up))
{
//test2.startanimation(IntRect(17,96,32,16),3,0.15,true,128);test2.sprite.move(0,-0.3);
}
if(Keyboard::isKeyPressed(Keyboard::Down)) test2.sprite.move(0,0.3);
test1.update(window);
test2.update(window);
test3.update(window);
test4.update(window);
test5.update(window);
test6.update(window);
test7.update(window);
test8.update(window);
window.display();
}
}