Programming 2D Games

The official forum for "Programming 2D Games" the book by: Charles Kelly

It is currently Sun Sep 22, 2019 4:35 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Wed Dec 27, 2017 6:59 pm 
Offline

Joined: Thu Oct 05, 2017 6:53 am
Posts: 13
Unless I'm misunderstanding, it appears to me that there isn't a way to have an Image where its 'startFrame' is larger than its 'endFrame'

I have some use cases where I'm interested in an animation being able to play in either direction

To make this happen, I changed the Image::update() function to this:
Code:
void Image::update(float frameTime)
{
    if (endFrame != startFrame)          // if animated sprite
    {
        animTimer += frameTime;             // total elapsed time
        if (animTimer > frameDelay)
        {
            animTimer -= frameDelay;
            if (startFrame < endFrame)
                currentFrame++;
            else
                currentFrame--;
            if ((currentFrame > endFrame && startFrame < endFrame) ||
                (currentFrame < endFrame && startFrame > endFrame))
            {
                if(loop == true)            // if looping animation
                    currentFrame = startFrame;
                else                        // not looping animation
                {
                    currentFrame = endFrame;
                    animComplete = true;    // animation complete
                }
            }
            setRect();                      // set spriteData.rect
        }
    }
}


And I added this simple helper function to image.h to allow easily reversing the animation:
Code:
// Reverse animation frames
virtual void reverseAnimation() { std::swap(startFrame, endFrame); }


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group