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

Author Topic: (SOLVED)window.pollEvent=>Only 1 action can be treated at once?(Move AND Rotate)  (Read 10256 times)

0 Members and 2 Guests are viewing this topic.

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
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.
« Last Edit: April 11, 2020, 11:26:43 am by Power »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
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
Laurent Gomila - SFML developer

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
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
Quote
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.

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
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.
« Last Edit: April 11, 2020, 03:14:48 pm by Power »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
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 ;)
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
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);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
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 :).

 

anything