Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: My animation class skips the last frame of an animation.  (Read 1099 times)

0 Members and 1 Guest are viewing this topic.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
My animation class skips the last frame of an animation.
« on: February 02, 2023, 05:08:00 pm »
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();  
       
        }
}



 
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: My animation class skips the last frame of an animation.
« Reply #1 on: February 02, 2023, 05:20:47 pm »
I suggest to use a debugger to step through your code one frame at a time.

Just briefly looking at the code I saw this part:
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 currentRect.left is equal to customXreturnpoint-currentRect.width it will execute both if-bodies and thus subtract two from remainingframes, maybe that leads to some skipping.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: My animation class skips the last frame of an animation.
« Reply #2 on: February 02, 2023, 05:36:03 pm »
Thanks for the quick reply.What do you suggest I do with the operators.I have been fiddling with those and although it makes a difference. I haven't figured out what's the right operators to use in this case.
Any other things you can spot?
« Last Edit: February 02, 2023, 05:38:15 pm by Me-Myself-And-I »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: My animation class skips the last frame of an animation.
« Reply #3 on: February 02, 2023, 05:44:53 pm »
No, a debugger is a tool that's built into your development environment or IDE, where you can set break points and the code execution will stop at the break point and then give you the option to move ahead one code line at a time.
I suggest you do a web search to learn more about it and how to use it.

Also sounds to me, like you haven't fully understood your own code. Maybe it's time to get out some pen and paper and think deeply about how it's supposed to work and then check with your code, for differences.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything