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

Author Topic: Strange behaviour with thor animations  (Read 1197 times)

0 Members and 1 Guest are viewing this topic.

deso

  • Newbie
  • *
  • Posts: 13
    • View Profile
Strange behaviour with thor animations
« on: November 09, 2015, 03:49:34 am »
So, I made my Robot objects hold their own thor::Animator objects, and made member functions to play the animations included in the thor::Animator object.

I keep pointers to my Robot objects inside a vector, which I pass as argument to my Game_Logic class, by copy if I'm not mistaken.

The thing is, for the first element of the robot vector, the animations play fine. For the second and subsequent robot objects, the animations only display the first frame, and get stuck there. The Robot objects are all exactly the same, so I have no clue what might be making this wrong.

It's hard to put some minimal code, because the sprites are generated by the Robot objects, which are then put together on a vector along with other sprites by Game_Logic, and given to Game_Screen to be drawn. I'll try to paste some relevant code

Oh, and the sf::Clock object I use is passed by reference to all objects. I dunno if it matters, though.

This is the innards of the Robot class

Robot::Robot()
{
        thor::FrameAnimation attack;
        attack.addFrame(1, sf::IntRect(60, 0, 60, 60));
        attack.addFrame(1, sf::IntRect(60, 60, 60, 60));
        attack.addFrame(1, sf::IntRect(60, 0, 60, 60));
        m_animator.addAnimation("attack", attack, sf::seconds(1));
}

sf::Sprite Robot::r_sprite(sf::Clock& clock)
{
        sf::Sprite sprite(m_texture, sf::IntRect(0, 0, 60, 60));       

        m_animator.update(clock.restart());    
        m_animator.animate(sprite);    
        sprite.setPosition(m_pos.x * 60, m_pos.y * 60);
        return sprite;
}

void Robot::play_anim(std::string anim)
{
        m_animator.playAnimation(anim);
}
 
This is the part of Game_Logic that gets the sprites:

std::vector<sf::Sprite> Game_Logic::r_drawables(sf::Clock& clock)
{
        std::vector<sf::Sprite> drawables;
        Entity* pl_entity;

        for (unsigned int i = 0; i < m_player_entities.size(); i++)
        {
                pl_entity = m_player_entities[i];
                drawables.push_back(pl_entity->r_sprite(clock));
        }
        for (unsigned int i = 0; i < m_AI_entities.size(); i++)
        {
                drawables.push_back(m_AI_entities[i]->r_sprite(clock));
        }
        return drawables;
}

And this is the part of Game_Screen that gets the sprites and draws stuff:

        m_drawables = m_game_logic->r_drawables(clock);
        for (unsigned int i = 0; i < m_drawables.size(); i++)
        {
                window->draw(m_drawables[i]);
        }
        window->display();

deso

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Strange behaviour with thor animations
« Reply #1 on: November 09, 2015, 04:43:15 am »
I think I figured it out. I was passing the whole sf::Clock object, and calling restart on every Robot object. Logically, the .restart() call was messing up with the numbers. I'll try calling .restart only once and sending the return value of that to the Robot objects.