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

Author Topic: SFML Beginners help, Moving sprite  (Read 1185 times)

0 Members and 1 Guest are viewing this topic.

ArtisanN1sT

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
SFML Beginners help, Moving sprite
« on: February 02, 2014, 09:51:23 pm »
Hello, I'm new to both SFML and this forum. I started learning SFML today so i decided to try implement some of the things I learned. Everything was fine until I used the .move function. Everything draws and runs but when the sprite moves it doesn't get rid of the previous state of the sprite, but keeps the drawn sprite.

My code:

#include <SFML/Graphics.hpp>
#include <iostream>


int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!", sf::Style::Fullscreen);

float x1 = 0;
float y1 = 725;
int x = 0;
int y = 800;


        window.setVerticalSyncEnabled(true);
        window.clear(sf::Color::Black);

        sf::Texture texture;

        if(!texture.loadFromFile("sprite.png", sf::IntRect(0,0,75,200))){

            std::cout<< "The sprite could not be loaded."<<std::endl;
        }

        sf::Texture sprite1;
        if(!sprite1.loadFromFile("Sprite2.png", sf::IntRect(140,110,80,80))){
            std::cout<<"Could not load texture."<<std::endl;
        }
        sf::Sprite sprites;
        sprites.setTexture(sprite1);

        sprites.setPosition(x1,y1);


        while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::KeyPressed){

                if(event.key.code == sf::Keyboard::Escape){
                window.close();
                }
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){


                sprites.move(5,0);



}
            }
        }

window.draw(sprites);
        sf::Sprite sprite;
        sprite.setTexture(texture);

        while( x < 1600){
        sprite.setPosition(sf::Vector2f(x,y));
        window.draw(sprite);
        x = x + 75;
}
        window.display();
    }

    return 0;
}

Sorry if it is messy, or unprofessional. I just really want to learn how to do this. Thank you!
 

ArtisanN1sT

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: SFML Beginners help, Moving sprite
« Reply #1 on: February 02, 2014, 09:56:41 pm »
Never mind, just figured it out. x3

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: SFML Beginners help, Moving sprite
« Reply #2 on: February 03, 2014, 02:43:31 pm »
Yes, you're supposed to clear the screen.

 

anything