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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - pinkahd

Pages: [1]
1
System / Thread running in a class stops when pressing a key
« on: October 24, 2013, 08:33:42 pm »
Hi there, i'm new to SFML and a novice to c++ so i came into a problem today when i try to build my own player class. The bug occurs when i press a key in a running thread, everything runs ok until i press a key any key, The function in the class have a while loop when it checks is a key is pressed and if it is then is displays a message in the console.

Here is the code with the problem if anyone knows what i did wrong can you explain it to me.

The player.h

#include "GlobalVariabiles.h"

class Player
{

public:

        Player(void);
        virtual ~Player(void);

        void setPlayerName(const std::string& name);
        void player_move();
       
        std::string getPlayerName();
        sf::Sprite renderPlayer();

private:       

        void setPlayerTexture(const std::string& path);

        sf::Texture player_texture;
        sf::Sprite player_sprite;
        sf::Thread move_thread;
       
};

 

And the player.cpp


#include "Player.h"

bool playerexist;
std::string Player_Name;

Player::Player(void)
        :move_thread(&Player::player_move, this)
{
        playerexist = true;
        std::cout << "A new Player have bean created !" << std::endl;
        this->move_thread.launch();
}

Player::~Player(void)
{
        playerexist = false;
        this->move_thread.terminate();
        //this->move_thread.~Thread();
}

void Player::player_move()
{

        while (playerexist){
                std::cout << "Runing";
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                                std::cout << "Player moved Up" << std::endl;
                        }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        {
                                std::cout << "Player moved Right" << std::endl;
                        }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        {
                                std::cout << "Player moved Down" << std::endl;
                        }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                                std::cout << "Player moved Left" << std::endl;                 
                        }      
        }

}

 

Thanks in advance.

Pages: [1]
anything