Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to move a sprite without leaving multiple copies?  (Read 2108 times)

0 Members and 1 Guest are viewing this topic.

bladelock

  • Newbie
  • *
  • Posts: 20
    • View Profile
How to move a sprite without leaving multiple copies?
« 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!

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
How to move a sprite without leaving multiple copies?
« Reply #1 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();
}

Haikarainen

  • Guest
How to move a sprite without leaving multiple copies?
« Reply #2 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

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
How to move a sprite without leaving multiple copies?
« Reply #3 on: September 12, 2011, 05:32:39 pm »
Whoops, forgot about the global input update. Thanks for correction.

 

anything