1
General / Input validation
« on: April 21, 2015, 08:28:55 pm »
Hi,
I have the following code which controls the players movement and shooting however I need a way to stop the player moving and shooting at the same time. I really don't know how to go about it.
I tried putting them all in else if's but this did not work.
Any Ideas?
Kind Regards
Andy
I have the following code which controls the players movement and shooting however I need a way to stop the player moving and shooting at the same time. I really don't know how to go about it.
I tried putting them all in else if's but this did not work.
void Game::handleEvents()
{
sf::Event event;
while (window.pollEvent(event))
{
// if the X is click or escape is pressed on the keyboard it will close the window
if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
window.close();
switch (event.type)
{
case sf::Event::KeyPressed:handleInput(event.key.code, true, temp);
break;
case sf::Event::KeyReleased:handleInput(event.key.code, false, temp);
default:
break;
}
}
}
void Game::handleInput(sf::Keyboard::Key keypress, bool keyPressed, sf::Clock temp)
{
if (keypress == sf::Keyboard::Up || keypress == sf::Keyboard::W)
{
pMoveUp = keyPressed;
}
else if (keypress == sf::Keyboard::Down || keypress == sf::Keyboard::S)
{
pMoveDown = keyPressed;
}
else if (keypress == sf::Keyboard::Right || keypress == sf::Keyboard::D)
{
pMoveRight = keyPressed;
}
else if (keypress == sf::Keyboard::Left || keypress == sf::Keyboard::A)
{
pMoveLeft = keyPressed;
}
if (keypress == sf::Keyboard::Space && keyPressed && lastFiredArrow.getElapsedTime() > sf::milliseconds(250))
{
// shoot stuff
}
}
{
sf::Event event;
while (window.pollEvent(event))
{
// if the X is click or escape is pressed on the keyboard it will close the window
if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
window.close();
switch (event.type)
{
case sf::Event::KeyPressed:handleInput(event.key.code, true, temp);
break;
case sf::Event::KeyReleased:handleInput(event.key.code, false, temp);
default:
break;
}
}
}
void Game::handleInput(sf::Keyboard::Key keypress, bool keyPressed, sf::Clock temp)
{
if (keypress == sf::Keyboard::Up || keypress == sf::Keyboard::W)
{
pMoveUp = keyPressed;
}
else if (keypress == sf::Keyboard::Down || keypress == sf::Keyboard::S)
{
pMoveDown = keyPressed;
}
else if (keypress == sf::Keyboard::Right || keypress == sf::Keyboard::D)
{
pMoveRight = keyPressed;
}
else if (keypress == sf::Keyboard::Left || keypress == sf::Keyboard::A)
{
pMoveLeft = keyPressed;
}
if (keypress == sf::Keyboard::Space && keyPressed && lastFiredArrow.getElapsedTime() > sf::milliseconds(250))
{
// shoot stuff
}
}
Any Ideas?
Kind Regards
Andy