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

Author Topic: checking for events  (Read 6146 times)

0 Members and 1 Guest are viewing this topic.

ahmads1990

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
checking for events
« on: March 29, 2020, 01:51:14 pm »
iam trying to make a menu
i need to check if the user pressed a button to move throw the menu
when i tried doing this function then calling it in main
if (Keyboard::isKeyPressed(Keyboard::S))
        {
                selectedText[selectedNum].setFillColor(Color::White);
                selectedNum++;
                selectedText[selectedNum].setFillColor(Color::Blue);
               
        }
the it goes throw all menu then the program crash
else if i use
if (event.type == Event::KeyPressed && event.key.code == Keyboard::S)
                                        menu.moveDown();
it works fine
 so what is the difference between these two ways what did ido wrong

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: checking for events
« Reply #1 on: March 30, 2020, 08:26:55 am »
iam trying to make a menu
i need to check if the user pressed a button to move throw the menu
when i tried doing this function then calling it in main
if (Keyboard::isKeyPressed(Keyboard::S))
        {
                selectedText[selectedNum].setFillColor(Color::White);
                selectedNum++;
                selectedText[selectedNum].setFillColor(Color::Blue);
               
        }
the it goes throw all menu then the program crash
else if i use
if (event.type == Event::KeyPressed && event.key.code == Keyboard::S)
                                        menu.moveDown();
it works fine
 so what is the difference between these two ways what did ido wrong

In the first code u're not relying on events. Probably ur index goes too high and this is why it crashes. The same happens on the second, but since it reacts on-event (not on-frame) it is executed fewer times and index doesn't reach the end