-
Hello,
I made a program where you can press the arrow keys to move an item, and if you press "Q" the item would rotate,
I noticed i could not have the item MOVE and rotate at the same time if i pressed an arrow key and 'Q' at the same time. Is that normal?
Here is the code :
while (window.isOpen())
{
...
...
sf::Event event;
while (window.pollEvent(event))
{
if(event.type==sf::Event::KeyPressed&&event.key.code==sf::Keyboard::Q)
{
pivot(RightObj,ang,sf::Vector2f(saveCenter1,saveCenter2),3);
pivot(LeftObj,ang,sf::Vector2f(saveCenter1,saveCenter2),3);
// save= ang+save;
}
if(event.type==sf::Event::KeyPressed&&event.key.code==sf::Keyboard::Right)
{
shiftVertex(RightObj,10,'r',3);
shiftVertex(LeftObj,10,'r',3);
}
...
...
}
It's quiiite frustrating, i want to make a reeally dynamic game where you can press many keys and ave many actions happen.
-
You don't want to react to events, but rather check the keyboard state at every iteration of your game loop. Read the documentation and tutorials again, this is well explained ;)
https://www.sfml-dev.org/tutorials/2.5/window-inputs.php
-
You don't want to react to events, but rather check the keyboard state at every iteration of your game loop. Read the documentation and tutorials again, this is well explained ;)
https://www.sfml-dev.org/tutorials/2.5/window-inputs.php
Ah perfect,
So you the code should be like this :
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
pivot(RightObj,ang,sf::Vector2f(saveCenter1,saveCenter2),3);
pivot(LeftObj,ang,sf::Vector2f(saveCenter1,saveCenter2),3);
}
So you use events only to check for important events that have to happen once, like jumping
This event is the one to use if you want to trigger an action exactly once when a key is pressed or released, like making a character jump with space, or exiting something with escape.
Quote from another tutorial actually : https://www.sfml-dev.org/tutorials/2.5/window-events.php
Thanks Laurent.
-
In case any person find this post by doing a research, i will explain how best the code should be done to allow fluid actions :
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{u=1;}
if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Up)
{u=0;}
}
/// outside of window.isOpen
if(u==1){
shiftVertex(CenterObj,10,'d',4);initialPositionY=initialPositionY-10;
shiftVertex(RightObj,10,'d',3);
shiftVertex(LeftObj,10,'d',3);
shiftVertex(UpperObj,10,'d',3);
shiftVertex(BottomObj,10,'d',3);}
}
Do this for every action.
It works great.
-
Your indentation is so messed up that it's impossible to see what code is inside the event loop or not.
When you post code for future readers, you should at least clean it a little bit ;)
-
You're mixing real-time input and events. Best to check "isKeyPressed" outside the event loop.
If you're using events, you can pair the KeyPressed event to the KeyReleased event.
But, if you're just using real-time input, you can do it like this:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
u = 1;
else
u = 0;
Or shorter:
u = (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ? 1 : 0);
-
Done.
You're mixing real-time input and events. Best to check "isKeyPressed" outside the event loop.
If you're using events, you can pair the KeyPressed event to the KeyReleased event.
But, if you're just using real-time input, you can do it like this:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
u = 1;
else
u = 0;
Or shorter:
u = (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) ? 1 : 0);
Oh much better, thank you Hapax :).