SFML community forums

Help => General => Topic started by: Sanction on August 10, 2015, 02:01:15 pm

Title: Tutorial switch help?[SOLVED]
Post by: Sanction on August 10, 2015, 02:01:15 pm
I'm sure this is a no brainer for some..

I'm getting a syntax error with: case sf::Event::KeyPressed: 'A';
Error says: case label value has already appeared in the switch

Is there a way to change the switch around to have multiple cases?
Thanks in advance!

CODE:
(click to show/hide)
Title: Re: Tutorial switch help?
Post by: G. on August 10, 2015, 02:25:53 pm
Your switch has 2 times the same case...:
case sf::Event::KeyPressed:

Your 'A' and 'W' don't mean anything and are useless.
Title: Re: Tutorial switch help?
Post by: Sanction on August 10, 2015, 02:34:25 pm
Yeah I get that..  I'm trying to find a solution and I just got a brain block when it comes to this switch statement. I can work it witch a if statement but its ugly
Title: Re: Tutorial switch help?
Post by: Jesper Juhl on August 10, 2015, 03:30:15 pm
switch (event.type) {
  ...
  case sf::Event::KeyPressed:
    switch (event.key.code) {
      ...
      case sf::Keyboard::Escape:
        // Escape was pressed
        break;
      ...
    }
    break;
  ...
}
 
Title: Re: Tutorial switch help?
Post by: Hapax on August 10, 2015, 03:38:10 pm
If an event has the type sf::Event::KeyPressed, it means that a key has been pressed; it could be any key!
To find out which key was pressed, you then need to check the event's field called key and its field called code, as shown in Jesper's rather clear example.

For more information on events, please read the tutorial about events (http://www.sfml-dev.org/tutorials/2.3/window-events.php)  ;)


EDIT: To address the specific question,
Quote
I'm getting a syntax error with: case sf::Event::KeyPressed: 'A';
This is incorrect.
'A'; is a separate statement (which does nothing) and is not evaluated as part of the case, as if you'd written:
case sf::Event::KeyPressed:
    'A';
This should help you visualise why it is saying you used the same case twice.
Title: Re: Tutorial switch help?
Post by: Sanction on August 10, 2015, 04:57:41 pm
Thank you guys! now I need to work in smoother movement.

CODE:
(click to show/hide)
Title: Re: Tutorial switch help?
Post by: Hapax on August 10, 2015, 06:02:19 pm
Thank you guys!
You're welcome.

now I need to work in smoother movement.
Good luck!  :)