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

Author Topic: sf::Event::Keypress problem  (Read 3115 times)

0 Members and 1 Guest are viewing this topic.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
sf::Event::Keypress problem
« on: April 27, 2016, 11:42:47 pm »
hello,
so i have this code
Paddle::Paddle()
{
        dir = sf::Keyboard::Unknown; // dir is of type sf::Keyboard::Key
}
void Paddle::update(bool isPlayer1,float delta)
{
    if ( isPlayer1 )
    {
        if ( dir == sf::Keyboard::Up  )
        {
            pad.move(0,(-speed)*delta);
        }
        if ( dir == sf::Keyboard::Down  )
        {
            pad.move(0,speed*delta);
        }
    }
    else
    {
        std::cout << dir << std::endl;
        if ( dir == sf::Keyboard::W  )
        {
            pad.move(0,(-speed)*delta);
        }
        if ( dir == sf::Keyboard::S  )
        {
            pad.move(0,speed*delta);
        }
    }
}

void Paddle::setDir(sf::Keyboard::Key key)
{
    dir = key;
}

 


on the Game class i have this

void Game::start()
{
while(window->isOpen())
{
  event();
  update();
  render();
}
}
void Game::event()
{
    sf::Event e;
    while(window->pollEvent(e))
    {
        if ( e.type == sf::Event::KeyPressed )
        {
            if ( e.key.code == sf::Keyboard::Up || e.key.code == sf::Keyboard::Down )
                player2.setDir(e.key.code); // player2 is a Paddle type
            else if ( e.key.code == sf::Keyboard::W || e.key.code == sf::Keyboard::S )
                player1.setDir(e.key.code); // player1 is a Paddle type
        }
        else if ( e.type == sf::Event::KeyReleased )
        {
            if ( e.key.code == sf::Keyboard::Up || e.key.code == sf::Keyboard::Down )
                player2.setDir(sf::Keyboard::Unknown);  // player2 is a Paddle type
            else if ( e.key.code == sf::Keyboard::W || e.key.code == sf::Keyboard::S )
                player1.setDir(sf::Keyboard::Unknown); // player1 is a Paddle type
        }
    }
}

void Game::update()
{
     player1.update(true,somedelta);
     player2.update(false,somedelta);
}
 

when ever i hold S key nothign happens.
the line
 std::cout << dir << std::endl;  
always output -1 which is its default value sf::Keyboard::Unknown it should output 18 which is sf::Keyboard::S
« Last Edit: April 27, 2016, 11:47:09 pm by lorence30 »

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: sf::Event::Keypress problem
« Reply #1 on: April 27, 2016, 11:47:27 pm »
EDITED

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: sf::Event::Keypress problem
« Reply #2 on: April 28, 2016, 12:03:18 am »
Events detect when the key was pressed.
If you want to check if the key is pressed, use sf::Keyboard::isKeyPressed(...)

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: sf::Event::Keypress problem
« Reply #3 on: April 30, 2016, 01:59:52 pm »
okay thanks for reply.

fixed , i just changed if ( isPlayer1 ) to if ( !isPlayer1 )

 

anything