简体   繁体   English

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

[英]How to lose control in few seconds?

At the starting of GameState.Running, i want to stop input key in 3 seconds, after that, every time when my character take damage, i want to lose control in 1 second, could anyone show me how to do that? 在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;
    }

You need some InputManager that you update only when you need. 您需要一些仅在需要时更新的InputManager。 Here is example of basic verstion of InputManager. 这是InputManager的基本版本示例。

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

new InputManager Class 新的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM