0
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ Yes, you can do it \$\endgroup\$ Commented Jan 26, 2016 at 0:28
  • \$\begingroup\$ how do you make it to constantly update after you have pressed the button. not update when its held down \$\endgroup\$ Commented Jan 26, 2016 at 0:33
  • \$\begingroup\$ This isn't really a game development related question. You are basically asking "How can I remember if something was true before?". \$\endgroup\$ Commented Jan 26, 2016 at 8:26

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
0
0
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.