3
votes

I'm completely new to sfml, so I was following a tutorial on youtube. I did exactly as the guy did but still I'm having issue with my movements.

Basically what's happening is that if I press a movement key the player (which is just a rectangle shape), moves just for a bit and when I release the key the player returns to it's official position. Why does it returns to it's official position. It just moves one time even if I press the key longer it doesn't move more than just one time.

here is my code:

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

using namespace std;
int main()
{

sf::RenderWindow window(sf::VideoMode(512, 512), "SFML Tutorial", sf::Style::Close | sf::Style::Titlebar| sf::Style::Resize);

//Game Loop

 while(window.isOpen())
    {

        //create an event
        sf::Event evnt;
       //create a rectangle shape called player
        sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
        player.setFillColor(sf::Color::Red);
        while(window.pollEvent(evnt))
            {
                switch(evnt.type)
                {
                case sf::Event::Closed:
                    window.close();
                    break;
                case sf::Event::Resized:
                    cout<<"New window width: "<<evnt.size.width<<" New window height: "<<evnt.size.height<<endl;
                    break;
                case sf::Event::TextEntered:
                    if(evnt.text.unicode < 128){
                    printf("%c", evnt.text.unicode);
                    }
                }

            } //window event

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
                {
                   player.move(-5.0f, 0.0f);
                }
            else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right))
                {
                    player.move(5.0f, 0.0f);
                }
            else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
                {
                    player.move(0.0f, -5.0f);
                }
            else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
                {
                    player.move(0.0f, 5.0f);
                }


            //display player

            window.clear();
            window.draw(player);
            window.display();
    }
    return 0;

}

1
Can you show us move function too? I think you coded it yourself - NixoN
This is what the guy did in the tutorial. This is everything related to movement code - Parwesh Bhaggan
I had followed his tutorial from beginning and this was his 5th part of the tutorial. The link is here: youtube.com/… - Parwesh Bhaggan
Can you give me full code? I got the idea - NixoN
That is the full code - Parwesh Bhaggan

1 Answers

2
votes

The problem is you creating a character every frame in 100.0f 100.0f in while loop. So, you need to move the code of creating a character out of while loop.

int main()
{
    sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f)); 
    player.setFillColor(sf::Color::Red);
    while(window.isOpen())
        {

             //create an event
             sf::Event evnt;
             //create a rectangle shape called player

             while(window.pollEvent(evnt))
             ...
        }
}