gpt4 book ai didi

c# - 如果按下键,如何保存一次点击?

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:02 25 4
gpt4 key购买 nike

当我按住 A 键时,熊一直在爆炸。

我想要的是,当我点击一次时只爆炸一个,然后等待再次按下以爆炸另一个。

        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);

最佳答案

XNA 键盘和按钮处理的惯例是维护旧 KeyboardState 的副本并将其与当前状态进行比较。您可以通过测试键当前是否按下但检查先前状态是否指示该键未按下来确定首次按下键的时间。

MSDN 示例:

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;
}

更改您的代码:

    if (bear0.Active && board.IsKeyDown(Keys.A))
{
bear0.Active = false;
boom.Play(bear0.DrawRectangle.Center.X, bear0.DrawRectangle.Center.Y);
}

...到:

    if (bear0.Active && board.IsKeyDown(Keys.A) && !oldState.IsKeyDown(Keys.A) )
{
bear0.Active = false;
boom.Play(bear0.DrawRectangle.Center.X, bear0.DrawRectangle.Center.Y);
}

...比查看熊的状态 bear0.Active 更好,因为爆炸熊后的框架会构造一个新的:

    else if (!bear0.Active)
{
bear0 = new TeddyBear(Content, WINDOW_WIDTH, WINDOW_HEIGHT, "teddybear0", newRandomX, newRandomY, myVector);
}

...这将在你的下一帧中爆炸 if (bear0.Active && board.IsKeyDown(Keys.A)

告诉我更多

关于c# - 如果按下键,如何保存一次点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29324417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com