0

I am a beginner in progamming in c++

And i tried to animate my character when i press the button.

so i wrote this :

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up||Down||Left||Right)
{...

i found a solution but it's compliquated

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)||sf::Keyboard::isKeyPressed(sf::Keyboard::Down)||sf::Keyboard::isKeyPressed(sf::Keyboard::Right)||sf::Keyboard::isKeyPressed(sf::Keyboard::Left))

I would like to know why my first try don't work ? and how i can do for do that simple as possible ?

Jules
  • 15
  • 4
  • 1
    Why do you think first solution would work? It seems you should start with [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You could add `using namespace sf::Keyboard;` to avoid repeating this part each and every time, but otherwise it's as simple as it can be (and as your API allows). You could also extract this to a function (i.e. `isArrowKeyPressed()`), then you'd just need to call this function. – Yksisarvinen Sep 25 '18 at 11:45
  • Also you can use `std::any_of`. – frogatto Sep 25 '18 at 12:02

2 Answers2

3

TL;DR: No there really no way around having to write your "complicated" condition.


The general syntax of the logical or operator is <expression> || <expression>.

With sf::Keyboard::Up||Down the left-hand side is indeed an expression, but the right-hand side isn't. It's just a symbol that probably aren't even declared, and as such will not compile.


The result of the logical or operator is a bool value, either true or false. The value true can be implicitly converted to 1 and false to 0. In the opposite direction all non-zero values are implicitly convertible to true while only 0 is convertible to false.

If we take the whole expression sf::Keyboard::Up||Down||Left||Right, and assume that the symbols would be valid without any scoping, the that expression is equal to ((sf::Keyboard::Up||Down)||Left)||Right.

Now as to how evaluate that expression, it depends on the values of those symbols. But if we assume that only one might possibly be zero, the we have

  • sf::Keyboard::Up||Down which will be true
  • true||Left which will be true
  • true||Right which will be true.

So your condition

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up||Down||Left||Right))

would really be

if(sf::Keyboard::isKeyPressed(true))

which is equal to

if(sf::Keyboard::isKeyPressed(1))

which using the Key enumeration would be equal to

if(sf::Keyboard::isKeyPressed(sf::Keyboard::B))
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Note: This answer only answers your second question. For the first question have a look at Some Programmer Dude's answer.

You can try it with a using namespace block:

int main() {
  // some code
  {
    using namespace sf::Keyboard;
    if(isKeyPressed(Up)    || isKeyPressed(Down) || 
       isKeyPressed(Right) || isKeyPressed(Left)) {
      //some stuff
    }
  }
  //more code
}

In easy words the using namespace sf::Keyboard adds sf::Keyboard to everything where the compiler thinks it makes sense (or better: it adds it if there is a function that fits in that namespace).

You can read up more here. At the bottom they use it pretty similar to what I did.

izlin
  • 2,129
  • 24
  • 30
  • Ok ty, but someone said me that us `using namespace` isn't advisable. 1 or 0 so ? – Jules Sep 25 '18 at 12:16
  • @Jules Somehow yes, but not (that much) in this case. [Here you can see why it is considered bad](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). It can be bad if you use it globally. If you are using a namespace inside of { }, you limit its use to this tiny space. The possibility that you get a misinterpretation is much smaller this way. You still have to be a bit mindful. – izlin Sep 25 '18 at 12:49