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

Author Topic: AnimatedSprite undefined behavior  (Read 2170 times)

0 Members and 1 Guest are viewing this topic.

cal

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
AnimatedSprite undefined behavior
« on: January 02, 2014, 02:05:05 pm »
I've implemented the AnimatedSprite class as suggested on the wiki, however the frames only animate correctly when I add a std::cout statement.

To keep this post relatively short (text-wise) I'll link to the code on github rather than pasting the files here.

main.cpp: https://github.com/natefailsalways/Pixels/blob/master/Pixels/Main.cpp

player.cpp: https://github.com/natefailsalways/Pixels/blob/master/Pixels/Player.cpp

player.h: https://github.com/natefailsalways/Pixels/blob/master/Pixels/Player.h

I just don't understand why the std::cout statement has any effect on the cycling of the sprites in walkingAnimation.

Here is the visual representation of the problem:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: AnimatedSprite undefined behavior
« Reply #1 on: January 02, 2014, 02:38:10 pm »
What happens if you limit the frame rate?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: AnimatedSprite undefined behavior
« Reply #2 on: January 02, 2014, 03:32:15 pm »
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        // close window
                        if (event.type == sf::Event::Closed)
                                window.close();
                        if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape)
                                window.close();        
                }
                window.clear();
                AnimatedSprite as = player.update(event);
                window.draw(ml);
                window.draw(as);
                //window.draw(Sprite);
                window.display();

        }

AnimatedSprite Player::update(sf::Event &event) {
        float speed = 45.0f; // 45px per second
        float delta = clock.restart().asSeconds();

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
                //as.setAnimation(walkingAnimation);
                animatedSprite.play(walkingAnimation);
                animatedSprite.move(0.0f, speed * delta);
                //std::cout << "hi" << std::endl; -- for some reason, this has to be uncommented to make the walkingAnimation work correctly.
        }
        if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Down){
                animatedSprite.setAnimation(stillDown);
                //as.pause();
        }
        animatedSprite.update(clock.restart());
        return animatedSprite;
}

  • Learn to use the event loop correctly, at the moment you may not even be handing a real event
  • Why does everyone like to convert a perfectly valid sf::Time to a float? Anyways in this case it shouldn't make a difference, but you should keep it as a sf::Time until you need to convert it to seconds.

Make these changes and let us know what happens.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

cal

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: AnimatedSprite undefined behavior
« Reply #3 on: January 04, 2014, 03:14:29 am »
First off, I just want to apologize for not replying in a timely manner. I got a new case yesterday and well, I was quite busy.

What happens if you limit the frame rate?

The framerate has no effect on player movement/animation due to it being calculated by speed*elapsed time.

 // code here

  • Learn to use the event loop correctly, at the moment you may not even be handing a real event
  • Why does everyone like to convert a perfectly valid sf::Time to a float? Anyways in this case it shouldn't make a difference, but you should keep it as a sf::Time until you need to convert it to seconds.

Make these changes and let us know what happens.

I've actually changed my code quite a bit and for now it seems I'm using my event loop correctly. I still have much to learn, my code is quite a mess I do apologize. I've managed to get it working:


Although, of course now the problem is syncing the hands/feet to the bobbing head motion. I think I'll take a short term break from this project to get a fresh perspective on where I should go from here with it. Thank you for your suggestion, it is much appreciated :)

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: AnimatedSprite undefined behavior
« Reply #4 on: January 04, 2014, 01:51:46 pm »
I just updated the code on the wiki and wrote a new example. Maybe that helps you too.

 

anything