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

Author Topic: Input validation  (Read 1385 times)

0 Members and 1 Guest are viewing this topic.

PapaSmurf

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
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.

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
        }

}
 

Any Ideas?

Kind Regards
Andy

Iggy

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Input validation
« Reply #1 on: April 21, 2015, 08:36:01 pm »
Could you check the players velocity and if its more or less than zero the player is moving so don't fire? Might be a cleaner solution.

Or set the player state to 'moving' and dont allow firing while in that state.

EG,
void Game::handleInput(sf::Keyboard::Key keypress, bool keyPressed, sf::Clock temp)
{

        if (keypress == sf::Keyboard::Up || keypress == sf::Keyboard::W)
        {
                pMoveUp = keyPressed;
                PlayerMoving = true;
        }
        else if(...)//... rinse repeat

        if (keypress == FIRE)
        {
                if (!PlayerMoving)
                {
                        //player isnt moving allow firing here
                }
        }
}

Don't include the firing button in your 'if else' if you use this.
« Last Edit: April 21, 2015, 08:45:18 pm by Iggy »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Input validation
« Reply #2 on: April 21, 2015, 08:57:56 pm »
I found this useful in a similar situation.