61
Graphics / How to move a sprite without leaving multiple copies?
« on: September 12, 2011, 02:57:42 pm »
You need to follow this logic:
-Process Events/Physics
-Then Clear the window from the previous frame
-Call Draw for the objects to be drawn
-Display
In your code it should be:
Some extra: You'll want to deal with real-time inputs since in your case you want to move the sprite, right?
For that you need to create the following condition outside the event loop (for SFML 2.0):
-Process Events/Physics
-Then Clear the window from the previous frame
-Call Draw for the objects to be drawn
-Display
In your code it should be:
Code: [Select]
while(Window.IsOpened())
{
sf::Event event;
while(Window.GetEvent(event))
{
if(event.Type == sf::Event::Closed)
{
Window.Close();
}
if((event.Type == Event.KeyPressed) && (event.Key.Code == Key::Left))
{
Sprite.Move(-10.0, 0);
}
}
Window.Clear()
Window.Draw(Sprite);
Window.Display();
}
Some extra: You'll want to deal with real-time inputs since in your case you want to move the sprite, right?
For that you need to create the following condition outside the event loop (for SFML 2.0):
Code: [Select]
while(Window.IsOpened())
{
sf::Event event;
while(Window.GetEvent(event))
{
if(event.Type == sf::Event::Closed)
{
Window.Close();
}
}
if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Left)
Sprite.Move(-10.0, 0);
Window.Clear()
Window.Draw(Sprite);
Window.Display();
}