SFML , key event problem.

So i am almost finished with my sfml snake game.
The only problem i have now is that after you lose, A prompt appears asking you if you want to exit(press escape), or try again(press r).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 if(gameOver){

        while(Window.waitEvent(event)){

                if(event.key.code==sf::Keyboard::Escape){
                    Window.close();

                }
                if(event.key.code==sf::Keyboard::R){
                    setup(gameSpeed);


                }
                break;
            }
        }

the problem i'm having is that the first time you lose the game, it works fine.
the second time you lose it doesn't even prompt you if you want to continue or exit, it just continues.
and so on until a crash happens.
if more of the code is needed,and you don't see a problem here, i will post it shortly.
Last edited on
More code is needed to help out with this problem. If you can post your whole project on here or pastebin/gist if it doesn't fit here. Otherwise if you don't want to paste the whole project the code that shows the prompt to the user when they lose and the code that shows what happens after the user presses continue (R) on the prompt should be enough to figure out the problem.
Two things, you should use pollEvent() not waitEvent() and you are breaking out of the event loop on the first iteration, which you shouldn't do, and don't need to with pollEvent().
http://pastebin.com/FzRS2b7a

here is the code for the whole game.
i commented out the parts that are probably causing the problems.

i also tried
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 if(gameOver){

        if(Window.pollEvent(event)){

                if(event.key.code==sf::Keyboard::Escape){
                    Window.close();

                }
                if(event.key.code==sf::Keyboard::R){
                    setup(gameSpeed);


                }

            }
        }


but it doesn't work either
Last edited on
it should be while (Window.pollEvent(event)){
There may be other errors, but that's important. pollEvent() returns true whenever there is an event in the queue stored by the window.
i tried it with while aswell. it doesn't work that way either
http://www.megafileupload.com/dsXq/sick_video_game.zip
here is a built version of the game you can try to see what actually happens at the end
I had a look (without the zip) and ran the source.
It seems to be when you press R to restart the game, it restarts fine but there is still loads of R press keyboard events to be looked at when you finish again so it will restart the game straight away.

So when the user presses the R key you need to restart the game and make sure all the other R key presses are ignored and then game continues.

Also it does crash because when you call update(Head) the game is over and the head is probably out of the bounds of some array.
So to stop it crashing I did:
1
2
3
4
5
6
if(elapsedTime>=gameSpeed)
{
if(!gameOver)update(Head)

//the rest of the code...
}


I did this without loading the images so it may look funkey - I dont know.

Edit: More importantly I found these problems by putting a breakpoint when gameOver=true;
and running line by line and looking at what happens in the while(Window.isOpen()) loop.

Happy debugging.
Last edited on
oh wow thank you xbv.
I still don't even know how to use break points.
Yes. If i'm not mistaken, without the images the snake head is just a white box,and the whole screen is black, but it still servers a debugging purpose.
Topic archived. No new replies allowed.