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

Author Topic: Multiple Key Inputs  (Read 4255 times)

0 Members and 1 Guest are viewing this topic.

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Multiple Key Inputs
« on: May 01, 2010, 09:35:50 pm »
Hello, everybody :]

I'm trying to get used to 2D game programming using the SFML library. My latest little tester program is just a skeleton at this point to enable one to shoot a little dot in four directions, depending on the last direction they moved in was.

I've noticed that when I move, I can only move in one direction at a time and it will also lock up sometimes if I press more than one key. I would like to know how I can change my code (or what I can add to it) to enable me to move in more than one direction and also to be able to move and fire my bullet at the same time. Below is the code for the part of my program that accepts input for the player structure:

Code: [Select]

void getInput(int &x, int &y, bool &shot, int &timer, int &direc)
{
   App.GetEvent(Event);
   
   if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
   {
      y--;
 if (!shot)
    direc = 1;
   }
   if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
   {
      x++;
 if (!shot)
    direc = 2;
   }
   if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
   {
      x--;
 if (!shot)
    direc = 4;
   }
   if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
   {
      y++;
 if (!shot)
    direc = 3;
   }
   
   if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Space))
   {
      if (timer <= 0)
 {
         shot = true;
timer = 500;
 }
   }
}


I would greatly appreciate any help in figuring out how to fix this. Thank you very much.

Colton
Whether you think you can or you can't, you're right.

4ian

  • Hero Member
  • *****
  • Posts: 680
    • View Profile
    • Game Develop website
Multiple Key Inputs
« Reply #1 on: May 01, 2010, 10:26:00 pm »
Use sf::Input instead of sf::Event for real-time event handling. ( Check the tutorial for more information : http://www.sfml-dev.org/tutorials/1.6/window-events.php )

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Multiple Key Inputs
« Reply #2 on: May 01, 2010, 10:31:16 pm »
Quote from: "4ian"
Use sf::Input instead of sf::Event for real-time event handling. ( Check the tutorial for more information : http://www.sfml-dev.org/tutorials/1.6/window-events.php )


Thank you very much, 4lan. It's no wonder now :p
Whether you think you can or you can't, you're right.