Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ok I need to know if it is possible to trigger a gameloop with a key press? ex code

 if (GamePad.GetState(PlayerIndex.One).Buttons.X == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Up))
        {
            ball.Update();
        }

obviously this is wrong because when I press the up button the ballstarts moving but stops when I release. and yes I looked online and couldn't find anything on this

share|improve this question
    
Yes, you can do it – rlam12 Jan 26 at 0:28
    
how do you make it to constantly update after you have pressed the button. not update when its held down – Shannessy Brown Jan 26 at 0:33
    
This isn't really a game development related question. You are basically asking "How can I remember if something was true before?". – Nils Ole Timm Jan 26 at 8:26
up vote 1 down vote accepted

Yes, make a boolean variable at the start in Game1. Like this:

if (ballStart)
{
    ball.update();
}

Next, go to where you get the input from the user and change the boolean to be true once the key is pressed. eg.

if (player1keyboardState.IsKeyDown(Keys.W))
{
    paddle1.direction.Y = -1;
    ballStart = true;
}

Hope this helps.

share|improve this answer
    
Thanks Man. :P . – Shannessy Brown Jan 26 at 2:18

This is possible. As for how to do it, there are many ways. The simplest is probably to use a boolean variable. When the input is read, set that variable to true. Have a while loop use that boolean flag as its condition. When you press the button, the flag is set to true, and the loop happens. When you want to end the loop, set the flag to false again.

Again, many ways to do it. But this one is almost trivially simple.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.