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

Author Topic: Animation doesn't loop  (Read 4712 times)

0 Members and 2 Guests are viewing this topic.

billboard_baggins

  • Newbie
  • *
  • Posts: 9
    • View Profile
Animation doesn't loop
« on: February 17, 2012, 11:52:26 pm »
Not sure if this belongs here, but, here is my issue: my little animation function doesn't loop. I have a class for "asteroids" and this member function animates a spinning motion (no user input required, happens every render cycle). Here is my code ("frames" is a vector array and "frame" is an iterator):

Code: [Select]

void Asteroid::animate()
{
    if ( frame == frames.end() ) frame = frames.begin();
    //if frame time has elapsed, move to next frame in animation
    if ( frameClock.GetElapsedTime().AsMilliseconds() >= frameTime.AsMilliseconds() )
    {
        mySprite.SetTextureRect(*frame); //set frame
        ++frame;
        //reset clock
        frameClock.Restart();
    }
}


The code does partially work, as it loops through the 7 frames once. However, after that the asteroid simply disappears (as if the 'frame = frames.begin()' isn't working). Any help is appreciated!

Elgan

  • Jr. Member
  • **
  • Posts: 77
    • AOL Instant Messenger - Flat+1,+17+st+Cl
    • View Profile
Animation doesn't loop
« Reply #1 on: February 17, 2012, 11:54:15 pm »
what happens after you reach max frames?

++frame? max Is?

Cpl.Bator

  • Hero Member
  • *****
  • Posts: 540
    • View Profile
Animation doesn't loop
« Reply #2 on: February 18, 2012, 12:41:27 am »
try to use front() instead of begin()

billboard_baggins

  • Newbie
  • *
  • Posts: 9
    • View Profile
Animation doesn't loop
« Reply #3 on: February 18, 2012, 01:01:38 am »
When it reaches max frames (there are 7 "frames" or sf::IntRects inside the "frames" vector) it is supposed to return to the first spot in the vector so the animation loops.

I tried setting frame = frames.front() but it spit out an error saying it wasn't a valid candidate.

Cpl.Bator

  • Hero Member
  • *****
  • Posts: 540
    • View Profile
Animation doesn't loop
« Reply #4 on: February 18, 2012, 01:12:16 am »
use frames[ index ] ?

billboard_baggins

  • Newbie
  • *
  • Posts: 9
    • View Profile
Animation doesn't loop
« Reply #5 on: February 18, 2012, 09:37:45 pm »
Using frames[index] gives me the same error. Logically the code makes sense to me so I'm really confused why it isn't working (unless my understanding of vector iterators is wrong...)

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Animation doesn't loop
« Reply #6 on: February 19, 2012, 03:45:41 am »
Quote
vector::end

"Returns an iterator referring to the past-the-end element in the vector container."

http://www.cplusplus.com/reference/stl/vector/end/


It seems end() is not really the last element. Try something like end() -1 ...
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

billboard_baggins

  • Newbie
  • *
  • Posts: 9
    • View Profile
Animation doesn't loop
« Reply #7 on: February 19, 2012, 06:25:13 am »
I just tried some variations of that but no luck. I also tried setting it to begin()+1 just to see if that made a difference, but it didn't. I'm convinced it has to do with the resetting of the iterator since it runs through the animation once perfectly fine.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Animation doesn't loop
« Reply #8 on: February 19, 2012, 09:20:31 am »
Code: [Select]
if ( frame == frames.end() ) frame = frames.begin();should be correct. Try to find out with the debugger why this if statement is not executed. Hint: frame - frames.begin() returns the iterator offset.


By the way,
Code: [Select]
frameClock.GetElapsedTime().AsMilliseconds() >= frameTime.AsMilliseconds() can be replaced with
Code: [Select]
frameClock.GetElapsedTime() >= frameTime
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

diegonolan

  • Newbie
  • *
  • Posts: 26
    • View Profile
Animation doesn't loop
« Reply #9 on: February 19, 2012, 11:23:56 am »
Where is frameTime changed?  I'm assuming this relates to the iterator frame?  Is it updated when the frame iterator is incremented?

billboard_baggins

  • Newbie
  • *
  • Posts: 9
    • View Profile
Animation doesn't loop
« Reply #10 on: February 19, 2012, 03:28:49 pm »
frameTime never changes, it just represents when to update the frame (in milliseconds). It is set when an asteroid object is instantiated.

Nexus, I will try the debugger and let you know. Also, thanks for the tip, I tend to write out more code than needed.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Animation doesn't loop
« Reply #11 on: February 19, 2012, 03:38:26 pm »
Based on the information I have I wrote a small test project.
It uses the function that you provided and it proves that there is nothing wrong with the frames, the code below works like it should.

The problem lies with your frameTime (which I didn't use) or somewhere else in your project.

Code: [Select]
struct Asteroid
{
    void animate()
    {
        if ( frame == frames.end() )
            frame = frames.begin();

            mySprite.SetTextureRect(*frame);
            ++frame;
        }
    }

    sf::Sprite mySprite;
    std::vector<sf::IntRect> frames;
    std::vector<sf::IntRect>::iterator frame;
};

int main()
{
    Asteroid test;
    test.frames.push_back(sf::IntRect(0, 0, 100, 100));
    test.frames.push_back(sf::IntRect(0, 0, 200, 200));
    test.frames.push_back(sf::IntRect(0, 0, 300, 300));
    test.frames.push_back(sf::IntRect(0, 0, 400, 400));

    test.frame = test.frames.begin();

   while (true)
   {
       test.animate();
       sf::Sleep(sf::Milliseconds(100));
   }
}
TGUI: C++ SFML GUI