Mixing real-time keyboard input (isKeypressed) into the event loop gets really messy quickly and can be relatively easily avoided.
First, you could check the real-time stuff together (outside of the event loop) and then set boolean 'flags' to control how to respond when an event is triggered. Very similar to your idea but without actually checking the key on every event.
Second, it could be better to do the event check first and, upon it being the correct event, then check to see if the other key is pressed (using either direct method or the method I mentioned above, for examples).
I envision this something like this:
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Keypressed:
switch (event.key.code)
{
case sf::Keyboard::W:
// is C pressed? do something for that. if not, do something else if you want to
break;
}
break;
}
}
}
However, looking at what your code seems to want do, it looks like you want to trigger throw when you press the key and then throw on the next key if still pressed.
Note that in your version, you are outputing text every frame that C is pressed.
So, you can use events for both (this is the code I'm suggesting you consider):
bool isReadyToThrow{ false };
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Keypressed:
switch (event.key.code)
{
case sf::Keyboard::C:
if (hasItem)
{
if (!isReadyToThrow)
std::cout << "Which direction?\n";
isReadyToThrow = true;
}
break;
case sf::Keyboard::W:
if (isReadyToThrow)
{
throwUp();
hasItem = false;
isReadyToThrow = false;
}
break;
case sf::Keyboard::S:
if (isReadyToThrow)
{
throwDown();
hasItem = false;
isReadyToThrow = false;
}
break;
case sf::Keyboard::A:
if (isReadyToThrow)
{
throwLeft();
hasItem = false;
isReadyToThrow = false;
}
break;
case sf::Keyboard::D:
if (isReadyToThrow)
{
throwRight();
hasItem = false;
isReadyToThrow = false;
}
break;
}
break;
case sf::Event::Keyreleased:
switch (event.key.code)
{
case sf::Keyboard::C:
isReadyToThrow = false;
break;
}
break;
}
}
}
Remember that events should be checked constantly so "throwUpwards()" should either be instantaneous or should trigger an action that would continue elsewhere (probably in an update section of the loop). It's usually best to avoid code that takes a long time to be triggered from with the event loop, stopping it from completing the event loop.
Is it just me that noticed that throwUp and throwDown have different meanings as well?