吕佩尔狐 2013-07-22 14:40 采纳率: 0%
浏览 969

c#新手,在一个xna模板中看到一个按键函数,看不懂,求前辈指点

public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer,
out PlayerIndex playerIndex)
{
if (controllingPlayer.HasValue)
{
// Read input from the specified player.
playerIndex = controllingPlayer.Value;

            int i = (int)playerIndex;

            return (CurrentKeyboardStates[i].IsKeyDown(key) &&
                    LastKeyboardStates[i].IsKeyUp(key));
        }
        else
        {
            // Accept input from any player.
            return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex) ||
                    IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
                    IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
                    IsNewKeyPress(key, PlayerIndex.Four, out playerIndex));
        }
    }

我不明白else中的return。
不是说out关键字是让函数来初始化参数的吗。但是给playerindex赋值的语句在if中,如果没有按键按下,那么if不成立,在else中这个函数会不断的自己调用自己,同时playerindex无法得到值。
本人刚接触编程,不懂怎么学习,理解能力不强,
以上是我的理解,这种理解无疑是错的。希望接触过编程的前辈给指点一下。

  • 写回答

1条回答

  • weixin_68789920 2023-01-09 10:23
    关注

    你的理解是正确的。这个函数中,playerIndex 在 if 块中被赋值,但是如果没有执行 if 块,那么 playerIndex 将不会被赋值。


    一个简单的修改方法是将 playerIndex 的初始值设置为 null,并在 else 块中返回 false。

    public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
    {
        playerIndex = null;
    
        if (controllingPlayer.HasValue)
        {
            // Read input from the specified player.
            playerIndex = controllingPlayer.Value;
    
            int i = (int)playerIndex;
    
            return (CurrentKeyboardStates[i].IsKeyDown(key) &&
                    LastKeyboardStates[i].IsKeyUp(key));
        }
        else
        {
            // Accept input from any player.
            return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex) ||
                    IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
                    IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
                    IsNewKeyPress(key, PlayerIndex.Four, out playerIndex));
        }
    }
    

    这样,如果 controllingPlayer 为 null,那么函数将返回 false,同时 playerIndex 也不会被赋值。


    另外,注意到函数中的递归调用。如果 controllingPlayer 为 null,那么函数会不断地调用自己,这可能会导致程序堆栈溢出。在这种情况下,递归调用应该替换为循环。

    public bool IsNewKeyPress(Keys key, PlayerIndex ? controllingPlayer, out PlayerIndex playerIndex)
    {
        playerIndex = null;
        if(controllingPlayer.HasValue)
        {
            // Read input from the specified player.
            playerIndex = controllingPlayer.Value;
            int i = (int) playerIndex;
            return(CurrentKeyboardStates[i].IsKeyDown(key) && LastKeyboardStates[i].IsKeyUp(key));
        }
        else
        {
            // Accept input from any player.
            PlayerIndex[] playerIndices = {
                PlayerIndex.One, PlayerIndex.Two, PlayerIndex.Three, PlayerIndex.Four
            };
            foreach(PlayerIndex index in playerIndices)
            {
                if(IsNewKeyPress(key, index, out playerIndex))
                {
                    return true;
                }
            }
            return false;
        }
    }
    

    这样,函数就可以正常工作了。

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序