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

Author Topic: Tutorial switch help?[SOLVED]  (Read 2658 times)

0 Members and 1 Guest are viewing this topic.

Sanction

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Tutorial switch help?[SOLVED]
« 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)
« Last Edit: August 11, 2015, 04:19:33 pm by Sanction »
Knowledge comes, but wisdom lingers. It may not be difficult to store up in the mind a vast quantity of facts within a comparatively short time, but the ability to form judgments requires the severe discipline of hard work and the tempering heat of experience and maturity.

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Re: Tutorial switch help?
« Reply #1 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.

Sanction

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Tutorial switch help?
« Reply #2 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
Knowledge comes, but wisdom lingers. It may not be difficult to store up in the mind a vast quantity of facts within a comparatively short time, but the ability to form judgments requires the severe discipline of hard work and the tempering heat of experience and maturity.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Tutorial switch help?
« Reply #3 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;
  ...
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3339
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Tutorial switch help?
« Reply #4 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  ;)


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.
« Last Edit: August 10, 2015, 03:49:20 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Sanction

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Tutorial switch help?
« Reply #5 on: August 10, 2015, 04:57:41 pm »
Thank you guys! now I need to work in smoother movement.

CODE:
(click to show/hide)
Knowledge comes, but wisdom lingers. It may not be difficult to store up in the mind a vast quantity of facts within a comparatively short time, but the ability to form judgments requires the severe discipline of hard work and the tempering heat of experience and maturity.

Hapax

  • Hero Member
  • *****
  • Posts: 3339
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Tutorial switch help?
« Reply #6 on: August 10, 2015, 06:02:19 pm »
Thank you guys!
You're welcome.

now I need to work in smoother movement.
Good luck!  :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*