Click here to Skip to main content
15,867,906 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here is my Key Input:
C#
if (Input.KeyPressed(Keys.P))
            {
                gameTimer.Stop();
                string paused = "Paused\n Press Enter to continue";
                lblGameOver.Text = paused;
                lblGameOver.Visible = true;
            }

            else
            {
                if (Input.KeyPressed(Keys.Enter))
                    gameTimer.Start();
                lblGameOver.Visible = false;
            }

Where did I miss? The game goes to Pause, but won`t resume...
Posted
Updated 24-Aug-15 7:08am
v3
Comments
CPallini 24-Aug-15 13:58pm    
Did you use a the debugger?
Googooli86 24-Aug-15 14:36pm    
Debugger gives no error, it pauses the game, but does not continue..
Henrik Jonsson 24-Aug-15 14:20pm    
I think you have to clarify which game engine you are using.

I suspect that after you have stopped the timer, the invocation of your code (script) above will also be stopped. Therefore, the "else" case will never be reached. You may have to implement cancellation by setting som state variable instead which must be considered before moving any game object.
Googooli86 24-Aug-15 14:39pm    
Hej Henrik,

I am using Visual Studio 2015. I am a bit confused right now.. I can now understand that "else" cannot be reached, but not right sure to be honest how to figure this implementing out..
gggustafson 24-Aug-15 14:39pm    
Suggest you remove the else and make if ( Input.KeyPressed ( Keys.Enter ) ) the start of a new conditional statement. Recall that the Input.Keypressed does not change between test.

The whole idea of using the timer for gaming is not the best. The newer approach is using window.requestAnimationFrame. Please see:
http://paulirish.com/2011/requestanimationframe-for-smart-animating[^].

Note the use of the fallback to window.setTimeout in the code sample.

The pausing of the game is pretty much trivial. You simply create some flag variable for paused/running state and don't process frames when the game is paused. You can see the complete code sample (sorry, it's not really minimal, is a fully-fledged game, but finding the technique is similar, please see states.paused in the code), for example, here: Tetris on Canvas[^].

—SA
 
Share this answer
 
v2
I figured it out by myself:

C#
private void Form1_KeyDown(object sender, KeyEventArgs e)
      {
          Input.ChangeState(e.KeyCode, true);

          if (Input.KeyPressed(Keys.P))
          {
              gameTimer.Stop();
          }

          if (Input.KeyPressed(Keys.Enter))
          {
              gameTimer.Start();
          }
      }


Had to be under Keydown
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900