【问题标题】:How to lose control in few seconds?如何在几秒钟内失去控制?
【发布时间】:2014-08-13 10:17:36
【问题描述】:

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

【问题讨论】:

    标签: c# xna keyboard keyboard-events


    【解决方案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;
    }
    

    【讨论】:

    • 我不确定“public static bool”,如何使用它,它们是如何工作的?
    • 您不需要 InputManager 的实例。您可以通过其类名直接使用它。 msdn.microsoft.com/en-us/library/79b3xss3.aspx
    • 我还有点小问题,如何删除键输入...如果我使用 'public override void Update(){ if(updateKeyboard) {InputManager.Update()} }' 键不会更新,但不会改变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    相关资源
    最近更新 更多