如果按键被按下,如何保存一键? [英] How to save one click if key is pressed?

查看:34
本文介绍了如果按键被按下,如何保存一键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按住A键时,熊总是爆炸.

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

推荐答案

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

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 示例:

newState 对象中的值与 oldState 对象中的值进行比较.在 newState 对象中按下但在 oldState 对象中未按下的键在此帧期间被按下.相反,在 oldState 对象中按下但在 newState 对象中未按下的键在此帧期间被释放.

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 因为在你炸掉熊之后你会构建一个新的框架:

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

...它会在你的下一个 if (bear0.Active && board.IsKeyDown(Keys.A)

...which will explode in the next frame during your next if (bear0.Active && board.IsKeyDown(Keys.A)

这篇关于如果按键被按下,如何保存一键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆