如何在几秒钟内失去控制?

时间:2014-08-11 02:33:00

标签: c# xna keyboard keyboard-events

在GameState.Running开始时,我想在3秒内停止输入键,之后,每当我的角色受到伤害时,我想在1秒内失去控制,有人能告诉我该怎么做吗?

        public override void Update(GameTime gameTime, KeyboardState Current, KeyboardState Old)
    {
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

        if (TimeGetReady < 0)
        {
            LockKey = false;
        }
        else
        {
            LockKey = true;
            TimeGetReady -= elapsed;
        }

        if (LockKey == false)
        {
            CurrentKeys = Current;
            OldKeys = Old;
        }


        if (CurrentKeys.IsKeyDown(Keys.P))
        {
            if (!OldKeys.IsKeyDown(Keys.P))
                if (IsPause == true)
                    IsPause = false;
                else
                    IsPause = true;
        }
        if (IsPause == true)
        {
            return;
        }
        Update(elapsed);
        if (CurrentKeys.IsKeyDown(Keys.Left))
        {

            KeyLeft = true;
            if (HPF < 3)
                HPF += 2;

            PushHorizontal += 5;
            Face = -1;

        }
        else
            if (Old.IsKeyDown(Keys.Left))
            {
                KeyLeft = false;
            }
        if (CurrentKeys.IsKeyDown(Keys.Right))
        {

            KeyRight = true;
            PushHorizontal += 5;
            if (HPF < 3)
                HPF += 2;
            Face = 1;

        }
        else
            if (Old.IsKeyDown(Keys.Right))
            {
                KeyRight = false;
            }

        if (KeyLeft == true || KeyRight == true)
        {
            KeyMove = true;
        }
        else
        {
            KeyMove = false;
        }
        if (KeyLeft == true && KeyRight == true)
        {
            KeyLeft = false;
            KeyRight = false;
            KeyMove = false;
        }
        if (CurrentKeys.IsKeyDown(Keys.X))
        {

            if (LockKeyX == false)
            {
                KeyJump = true;
                LockKeyX = true;
                PushVertical += 500;
            }
            if (VPF < 8)
                VPF += 3;

        }
        else
            if (Old.IsKeyDown(Keys.X))
            {
                KeyJump = false;
            }
        if (CurrentKeys.IsKeyUp(Keys.X))
        {
            if (Down == true)
            {
                LockKeyX = false;
            }
        }
        if (CurrentKeys.IsKeyDown(Keys.Z))
        {

            if (LockKeyZ == false)
            {
                KeyDash = true;
                KeyJump = false;
                LockKeyZ = true;
                PushHorizontal += 100;
            }
            if (KeyDash == true)
            {
                HPF = 1;
                PushVertical += Gravity;
                VPF = Gravity;
            }

        }
        else
            if (Old.IsKeyDown(Keys.Z))
            {
                KeyDash = false;
            }
        if (CurrentKeys.IsKeyUp(Keys.Z))
        {
            if (KeyMove == false && Down == true)
                LockKeyZ = false;
        }
        if (CurrentKeys.IsKeyDown(Keys.C))
        {
            ChargeTime += elapsed;
            if (LockKeyC == false)
            {
                LockKeyC = true;
                TimeShot += 10f;
                StayShot.ResetFrame();
            }
        }
        else
            if (Old.IsKeyDown(Keys.C))
            {
                if (ChargeTime > 0)
                {
                    TimeShot += 0.4f;
                    ChargeTime = 0;
                }
                LockKeyC = false;
            }
        Update(elapsed);
        UpdateInteraction();
        if (TimeGetReady <= 0)
        {
            UpdateStatus(gameTime);
        }
        UpdateFrameStatus(elapsed);
        LastStatus = RockmanStatus;
    }

1 个答案:

答案 0 :(得分:1)

您需要一些仅在需要时更新的InputManager。以下是InputManager基本版本的示例。

public override void Update(){
    if(updateKeyboard) {InputManager.Update()}
}

新的InputManager类

public static class InputManager
{
    public static void Update()
    {
        _previousKeyboardState = _currentKeyboardState;
        _currentKeyboardState = Keyboard.GetState();
    }

    public static bool IsKeyDown(Keys key) 
    {
        return _currentKeyboardState.IsKeyDown(key);
    }

    public static bool IsKeyUp(Keys key)
    {
        return _currentKeyboardState.IsKeyUp(key);
    }

    public static bool OnKeyDown(Keys key)
    {
        return _currentKeyboardState.IsKeyDown(key) && _previousKeyboardState.IsKeyUp(key);
    }

    public static bool OnKeyUp(Keys key)
    {
        return _currentKeyboardState.IsKeyUp(key) && _previousKeyboardState.IsKeyDown(key);
    }

    private static KeyboardState _currentKeyboardState;
    private static KeyboardState _previousKeyboardState;
}