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