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

Author Topic: movement without keyboard input  (Read 1339 times)

0 Members and 1 Guest are viewing this topic.

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
movement without keyboard input
« on: October 08, 2013, 07:25:07 pm »
in this code i tried to make a basic circle move via wasd key combo, but as soon as i start it up the circle starts moving to the left without keyboard input?

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

class Control{
    public:
        sf::RenderWindow window;
        sf::CircleShape circle;
        bool move_up, move_down, move_left, move_right;
        //int speed = 1;
       
        Control()
        : window(sf::VideoMode(600, 480), "test")
        , circle(){
            circle.setRadius(40.f);
            circle.setPosition(100.f, 100.f);
            circle.setFillColor(sf::Color::Cyan);
        }
        void run(){
            while (window.isOpen()){
                process_events();
                update();
                render();
            }
        }
        void process_events(){
            sf::Event event;
            while (window.pollEvent(event)){
                if (event.type == sf::Event::Closed){
                    window.close();
                }
                else if (event.type == sf::Event::KeyPressed){
                    handler(event.key.code, true);
                }
                else if (event.type == sf::Event::KeyReleased){
                    handler(event.key.code, false);
                }
               
            }
        }
        void handler(sf::Keyboard::Key key, bool is_pressed){
            if (key == sf::Keyboard::W)
                move_up = is_pressed;
            else if (key == sf::Keyboard::S)
                move_down = is_pressed;
            else if (key == sf::Keyboard::A)
                move_left = is_pressed;
            else if (key == sf::Keyboard::D)
                move_right = is_pressed;
        }
       
        void update(){
            sf::Vector2f movement(0.f, 0.f);
            if (move_up)
                movement.y -= 1.f;
            if (move_down)
                movement.y += 1.f;
            if (move_left)
                movement.x -= 1.f;
            if (move_right)
                movement.x += 1.f;
               
            circle.move(movement);
        }
       
        void render(){
            window.clear();
            window.draw(circle);
            window.display();
        }
   
};

int main(){
    Control app;
    app.run();
}
 
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

Gordon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: movement without keyboard input
« Reply #1 on: October 08, 2013, 07:50:13 pm »
You have to init your booleans move_up, move_down, move_left and move_right in the constructor with false. Otherwise the value is not defined.

regards,
Gordon

metulburr

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: movement without keyboard input
« Reply #2 on: October 08, 2013, 07:56:24 pm »
oh im stupid, thanks
OS Ubuntu 13.04, Arch Linux, Gentoo, Windows 7/8
SFML 2.1
Ati Radeon HD 6770

 

anything