问题
When I hold the key A, bears explode all the time.
What I want, is when I click once explode only one, and wait for another press to explode another one.
KeyboardState board = Keyboard.GetState();
int newRandomX = rand.Next(800);
int newRandomY = rand.Next(600);
float newVelocityX = rand.Next(-5,5);
float newVelocityY = rand.Next(-5,5);
Vector2 myVector = new Vector2(newVelocityX, newVelocityY);
if (bear0.Active && board.IsKeyDown(Keys.A))
{
bear0.Active = false;
boom.Play(bear0.DrawRectangle.Center.X, bear0.DrawRectangle.Center.Y);
}
else if (bear1.Active && board.IsKeyDown(Keys.B) && !oldState.IsKeyDown(Keys.B))
{
bear1.Active = false;
boom.Play(bear1.DrawRectangle.Center.X, bear1.DrawRectangle.Center.Y);
}
else if (!bear1.Active)
{
bear1 = new TeddyBear(Content, WINDOW_WIDTH, WINDOW_HEIGHT, "teddybear1", newRandomX, newRandomY, myVector);
}
else if (!bear0.Active)
{
bear0 = new TeddyBear(Content, WINDOW_WIDTH, WINDOW_HEIGHT, "teddybear0", newRandomX, newRandomY, myVector);
}
board = oldState;
bear0.Update();
bear1.Update();
boom.Update(gameTime);
回答1:
Convention in XNA keyboard and button handling is to maintain a copy of the old KeyboardState
and compare it to the current state. You determine when the key is first pressed by testing if the key is currently down but checking that the prior state indicated that the key was not down.
MSDN example:
Compare the values in your newState object to the values in the oldState object. Keys pressed in the newState object that were not pressed in the oldState object were pressed during this frame. Conversely, keys pressed in the oldState object that are not pressed in the newState object were released during this frame.
private void UpdateInput()
{
KeyboardState newState = Keyboard.GetState();
// Is the SPACE key down?
if (newState.IsKeyDown(Keys.Space))
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(Keys.Space))
{
backColor =
new Color(backColor.R, backColor.G, (byte)~backColor.B);
}
}
else if (oldState.IsKeyDown(Keys.Space))
{
// Key was down last update, but not down now, so
// it has just been released.
}
// Update saved state.
oldState = newState;
}
Change your code from:
if (bear0.Active && board.IsKeyDown(Keys.A))
{
bear0.Active = false;
boom.Play(bear0.DrawRectangle.Center.X, bear0.DrawRectangle.Center.Y);
}
...to:
if (bear0.Active && board.IsKeyDown(Keys.A) && !oldState.IsKeyDown(Keys.A) )
{
bear0.Active = false;
boom.Play(bear0.DrawRectangle.Center.X, bear0.DrawRectangle.Center.Y);
}
...is better than looking at the bear's state bear0.Active
because the frame after you explode the bear you constuct a new one:
else if (!bear0.Active)
{
bear0 = new TeddyBear(Content, WINDOW_WIDTH, WINDOW_HEIGHT, "teddybear0", newRandomX, newRandomY, myVector);
}
...which will explode in the next frame during your next if (bear0.Active && board.IsKeyDown(Keys.A)
Tell Me More
- Detecting a Key Press (Windows, Windows Phone, Xbox 360)
来源:https://stackoverflow.com/questions/29324417/how-to-save-one-click-if-key-is-pressed