输入状态方法参数

本文关键字:参数 方法 状态 输入 | 更新日期: 2023-09-27 18:34:13

查看XNA GSM套件的一些输入,我注意到一些 booleans有两个参数,而其他参数只有一个。为什么?两者之间有区别吗?

下面是两个示例,后跟指向整个代码的链接。

        /// <summary>
    /// Checks for a "pause the game" input action.
    /// The controllingPlayer parameter specifies which player to read
    /// input for. If this is null, it will accept input from any player.
    /// </summary>
    public bool IsPauseGame(PlayerIndex? controllingPlayer)
    {
        PlayerIndex playerIndex;
        return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
               IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex) ||
               IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
    }

    /// <summary>
    /// Checks for a "menu cancel" input action.
    /// The controllingPlayer parameter specifies which player to read input for.
    /// If this is null, it will accept input from any player. When the action
    /// is detected, the output playerIndex reports which player pressed it.
    /// </summary>
    public bool IsMenuCancel(PlayerIndex? controllingPlayer,
                             out PlayerIndex playerIndex)
    {
        return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
               IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex) ||
               IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex);
    }

完整输入状态代码

输入状态方法参数

第一个函数告诉你玩家按下了暂停按钮,但没有告诉你是哪一个。第二个函数告诉您玩家取消了菜单,并告诉是哪个玩家取消了菜单。这是此函数的out PlayerIndex playerIndex参数。

基本上,开发人员选择传递或不传递他们从输入检测函数收到的信息。

至于为什么,我想重要的是要知道哪个玩家关闭菜单。例如,在PES中,每个玩家都设置他的设置,然后按取消菜单按钮。菜单实际上只有在两个玩家都按下取消按钮时才会关闭。

猜,关于谁要求暂停的信息无关紧要,因此不会传递。

如果要指定要查找输入的特定人员,则使用控制播放器。 如果未指定,它将查看所有控制器。玩家索引指定谁按下了提供的输入。这就是为什么它是一个 out 参数。