Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.
Moderator: Coders of Rage
Daxtorax
ES Beta Backer
Posts: 26 Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++
Post
by Daxtorax » 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:
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.
Daxtorax
ES Beta Backer
Posts: 26 Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++
Post
by Daxtorax » Mon Apr 20, 2009 8:38 pm
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.
MarauderIIC
Respected Programmer
Posts: 3406 Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA
Post
by MarauderIIC » Tue Apr 21, 2009 3:30 am
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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
K-Bal
ES Beta Backer
Posts: 701 Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:
Post
by K-Bal » Tue Apr 21, 2009 8:24 am
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
RyanPridgeon
Chaos Rift Maniac
Posts: 447 Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:
Post
by RyanPridgeon » Tue Apr 21, 2009 5:28 pm
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.
Ryan Pridgeon
C ,
C++ ,
C# ,
Java ,
ActionScript 3 ,
HaXe ,
PHP ,
VB.Net ,
Pascal
Music | Blog
K-Bal
ES Beta Backer
Posts: 701 Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:
Post
by K-Bal » Tue Apr 21, 2009 6:36 pm
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.
Daxtorax
ES Beta Backer
Posts: 26 Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++
Post
by Daxtorax » Tue Apr 21, 2009 7:15 pm
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.