读书人

c#生手在一个xna模板中看到一个按键

发布时间: 2013-08-01 15:23:18 作者: rapoo

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无法得到值。
本人刚接触编程,不懂怎么学习,理解能力不强,
以上是我的理解,这种理解无疑是错的。希望接触过编程的前辈给指点一下。 C# XNA 编程
------解决方案--------------------


仔细看,if判断的是controllingPlayer是否有值
没有值的情况下调用的IsNewKeyPress的controllingPlayer是有值的,所以不会死循环。

读书人网 >C#

热点推荐