1
General / Re: Keyboard problem
« on: October 10, 2013, 06:46:05 pm »
I found the fix! Sorry if I wasted anyone's time. I forgot to set the mIsMovingUp, etc. to false.
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.
void Game::run(){
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (mWindow.isOpen()){
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame){
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(TimePerFrame);
}
render();
}
}
void Game::processEvents(){
sf::Event event;
while (mWindow.pollEvent(event)){
switch (event.type){
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
mWindow.close();
break;
}
}
}
void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed){
if (key == sf::Keyboard::W)
mIsMovingUp = isPressed;
else if (key == sf::Keyboard::S)
mIsMovingDown = isPressed;
else if (key == sf::Keyboard::A)
mIsMovingLeft = isPressed;
else if (key == sf::Keyboard::D)
mIsMovingRight = isPressed;
}
void Game::update(sf::Time deltaTime){
sf::Vector2f movement(0.f, 0.f);
if (mIsMovingUp)
movement.y -= PlayerSpeed;
if (mIsMovingDown)
movement.y += PlayerSpeed;
if (mIsMovingLeft)
movement.x -= PlayerSpeed;
if(mIsMovingRight)
movement.x += PlayerSpeed;
mPlayer.move(movement * deltaTime.asSeconds());
}