SFML community forums

Help => System => Topic started by: ahmads1990 on March 29, 2020, 01:51:14 pm

Title: checking for events
Post by: ahmads1990 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
Title: Re: checking for events
Post by: Fx8qkaoy 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