SFML community forums

Help => Graphics => Topic started by: bladelock on September 12, 2011, 02:47:13 pm

Title: How to move a sprite without leaving multiple copies?
Post by: bladelock on September 12, 2011, 02:47:13 pm
Well,  i was trying to make my sprite move to the left, however, Not only does it move to the left, it leaves a copy of the sprite at its former place.

How do i just let it move to the left WITHOUT leaving a copy of the sprite at its former place?

Here is my code:

Code: [Select]
#define SFML_STATIC

#include<iostream>
#include<SFML\System.hpp>
#include<SFML\Graphics.hpp>
using namespace std;
using namespace sf;

int main()
{

Image Image;


if (!Image.LoadFromFile("ToadR.png"))
return 1;

VideoMode Vmode(800,600,32);
RenderWindow Window(Vmode, "I am Awesome.");

Sprite Sprite;
Sprite.SetImage(Image);
float x = 400.0;
float y = 300.0;
Sprite.SetPosition(x, y);
Event Event;



while(Window.IsOpened())
{
Window.Draw(Sprite);
Window.Display();


while(Window.GetEvent(Event))
{
if(Event.Type == Event.Closed)
{
Window.Close();
}
else if((Event.Type == Event.KeyPressed) && (Event.Key.Code == Key::Left))
{

Sprite.Move(-10.0, 0);




}

}




}
return 0;
}




I'm stumped. I hope I could get help. Thanks!
Title: How to move a sprite without leaving multiple copies?
Post by: Contadotempo 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:
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();
}
Title: How to move a sprite without leaving multiple copies?
Post by: Haikarainen on September 12, 2011, 04:49:19 pm
contadotempo is right, but hes missing one thing about direct input. You'll need to check if the window has focus first, or else you will be able to move your sprite independent of wich window you have open at the moment.

before loop:
Code: [Select]
bool HasFocus = true;

in eventloop:
Code: [Select]
if(event.Type == sf::Event::GainedFocus){
HasFocus = true;
}

if(event.Type == sf::Event::LostFocus){
HasFocus = false;
}



then on move:
Code: [Select]
if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Left) && HasFocus){
      Sprite.Move(-10.0, 0);
}


This is only relevant to 2.0
Title: How to move a sprite without leaving multiple copies?
Post by: Contadotempo on September 12, 2011, 05:32:39 pm
Whoops, forgot about the global input update. Thanks for correction.