C++ and SFML Character Animation
Posted: Mon Apr 20, 2009 8:36 pm
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:
Any help is greatly appreciated, and thank you for taking your time.
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;
}