• WP7备注(3)(GamePad)


    public class InputState
        {
            public const int MaxInputs = 4;
    
            public readonly GamePadState[] CurrentGamePadStates;
            public readonly GamePadState[] LastGamePadStates;
    
            public InputState()
            {
                CurrentGamePadStates = new GamePadState[MaxInputs];
                LastGamePadStates = new GamePadState[MaxInputs];
            }
    
            public bool MenuUp
            {
                get
                {
                    return IsTargetButtonPress(Buttons.DPadUp);
                }
            }
    
            public bool MenuDown
            {
                get
                {
                    return IsTargetButtonPress(Buttons.DPadDown);
                }
            }
    
            public bool MenuSelect
            {
                get
                {
                    return IsTargetButtonPress(Buttons.A);
                }
            }
    
            public bool MenuCancel
            {
                get
                {
                    return IsTargetButtonPress(Buttons.Back);
                }
            }
    
            public bool PauseGame
            {
                get
                {
                    return IsTargetButtonPress(Buttons.Back) ||
                           IsTargetButtonPress(Buttons.Start);
                }
            }
    
            public void Update()
            {
                for (int i = 0; i < MaxInputs; i++)
                {
                    LastGamePadStates[i] = CurrentGamePadStates[i];
                    CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i, GamePadDeadZone.IndependentAxes);
                }
            }
    
            public bool IsTargetButtonPress(Buttons button)
            {
                for (int i = 0; i < MaxInputs; i++)
                {
                    if (IsTargetButtonPress(button, (PlayerIndex)i))
                        return true;
                }
    
                return false;
            }
    
            public bool IsTargetButtonPress(Buttons button, PlayerIndex playerIndex)
            {
                return (CurrentGamePadStates[(int)playerIndex].IsButtonDown(button) && LastGamePadStates[(int)playerIndex].IsButtonUp(button));
            }
    
            public bool IsMenuSelect(PlayerIndex playerIndex)
            {
                return IsTargetButtonPress(Buttons.A, playerIndex) || IsTargetButtonPress(Buttons.Start, playerIndex);
            }
    
            public bool IsMenuCancel(PlayerIndex playerIndex)
            {
                return IsTargetButtonPress(Buttons.B, playerIndex) || IsTargetButtonPress(Buttons.Back, playerIndex);
            }
        }
     
    整个输入状态类是通过以下方法实现的:
    public bool IsTargetButtonPress(Buttons button)
            {
                for (int i = 0; i < MaxInputs; i++)
                {
                    if (IsTargetButtonPress(button, (PlayerIndex)i))
                        return true;
                }
    
                return false;
            }
    
            public bool IsTargetButtonPress(Buttons button, PlayerIndex playerIndex)
            {
                return (CurrentGamePadStates[(int)playerIndex].IsButtonDown(button) && LastGamePadStates[(int)playerIndex].IsButtonUp(button));
            }
    

    共有两个方法

    1.一是判断是否有人输入了这个键(针对任何人)

    2.二是判断是否这个指定人输入了这个键(针对特定的一个人)

     

    每次获取用户所按的键使用过一下方法:

    public void Update()
            {
                for (int i = 0; i < MaxInputs; i++)
                {
                    LastGamePadStates[i] = CurrentGamePadStates[i];
                    CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i, GamePadDeadZone.IndependentAxes);
                }
            }

  • 相关阅读:
    TCP的三次握手与四次挥手理解及面试题(很全面)
    python解释器锁的理解
    Flask的基本使用、四剑客和配置文件
    Django cache缓存
    xadmin后台管理
    cookies与session
    Java stream流
    Java IO流
    springboot配置文件加载顺序与一些常用配置
    OAuth2.0开放授权
  • 原文地址:https://www.cnblogs.com/otomii/p/2029118.html
Copyright © 2020-2023  润新知