Page 1 of 1

C++ and SFML Character Animation

Posted: Mon Apr 20, 2009 8:36 pm
by Daxtorax
I have recently started learning SFML and I am trying to make the character's legs move when it's moving across the screen. It compiles fine and the character is able to move across the screen and yet it doesn't animate the legs. Oh and I am using Code::Blocks.
The code:

Code: Select all

#include <SFML/Graphics.hpp>
#include<iostream>


 int main()
 {
     // Create the main window
     sf::RenderWindow App(sf::VideoMode(800, 600), "Game");

     // Sprite Images
    sf::Image Image;
    if (!Image.LoadFromFile("character1.png"))
        return EXIT_FAILURE;
    sf::Image Image2;
    if (!Image2.LoadFromFile("character2.png"))
        return EXIT_FAILURE;
    sf::Image Image3;
    if (!Image3.LoadFromFile("character3.png"))
        return EXIT_FAILURE;
    sf::Sprite Sprite(Image);

     // Start the game loop
     while (App.IsOpened())
     {
         // Process events
         sf::Event Event;
         const sf::Input& Input = App.GetInput();
         int spriten = 0;
         sf::View View;
         while (App.GetEvent(Event))
         {
             // Close window : exit
             if (Event.Type == sf::Event::Closed)
             {
                 App.Close();
             }
             if (Input.IsKeyDown(sf::Key::Up))
             {
                 Sprite.Move(0, -5);
                 App.Draw(Sprite);
             }
             if (Input.IsKeyDown(sf::Key::Down))
             {
                 Sprite.Move(0, 5);
                 spriten++;
                 if (spriten <= 5)
                 {
                     Sprite.SetImage(Image2);
                     App.Draw(Sprite);
                 }
                 else if ((spriten >= 10) && (spriten <= 15))
                 {
                     Sprite.SetImage(Image);
                     App.Draw(Sprite);
                 }
                 else if (spriten >= 16)
                 {
                     Sprite.SetImage(Image3);
                     App.Draw(Sprite);
                     spriten = 0;
                 }
                 App.Draw(Sprite);
             }
             if (Input.IsKeyDown(sf::Key::Left))
             {
                 Sprite.Move(-5, 0);
                 App.Draw(Sprite);
             }
             if (Input.IsKeyDown(sf::Key::Right))
             {
                 Sprite.Move(5, 0);
                 App.Draw(Sprite);
             }

         }

         // Clear screen
         App.Clear();

         // Draw the sprite
         App.Draw(Sprite);

         // Update the window
         App.Display();
     }

     return EXIT_SUCCESS;
 }
Any help is greatly appreciated, and thank you for taking your time.

Re: C++ and SFML Character Animation

Posted: Mon Apr 20, 2009 8:38 pm
by Daxtorax
Oh, I also forgot to mention that, I realize that even with the code I currently have it would only animate him when he's moving down, I just want to at least get the concept of how it works. Thanks again.

Re: C++ and SFML Character Animation

Posted: Tue Apr 21, 2009 3:30 am
by MarauderIIC
Took 10 seconds to look through it. Looks like spriten controls the frame, or something? Every time the loop iterates spriten is reinitialized to 0, but you treat it as though iterating through the loop should have an effect (you increment it, but then the loop continues again and then it's reset to 0). You should declare and initialize spriten outside the while loop.

Re: C++ and SFML Character Animation

Posted: Tue Apr 21, 2009 8:24 am
by K-Bal
A cleaner way of doing animations would be to use the animated sprite class from the SFML-Wiki or writing a class for it yourself ;)

Ciao,
Marius

Re: C++ and SFML Character Animation

Posted: Tue Apr 21, 2009 5:28 pm
by RyanPridgeon
That is very strange. It looks to me like your code SHOULD work...

Maybe you're having problems with Sprite.SetImage() ?

I don't know how SFML works, so I am only guessing at how Image and Sprite are used.

Re: C++ and SFML Character Animation

Posted: Tue Apr 21, 2009 6:36 pm
by K-Bal
RyanPridgeon wrote:That is very strange. It looks to me like your code SHOULD work...

Maybe you're having problems with Sprite.SetImage() ?

I don't know how SFML works, so I am only guessing at how Image and Sprite are used.
MarauderIIC already gave the important hint.

Re: C++ and SFML Character Animation

Posted: Tue Apr 21, 2009 7:15 pm
by Daxtorax
Thanks guys, I figured it out, and fixed up my code a bit. I'm able to animate my character fine now, I'll also look into the animated sprite class.