0

我有

public string[] ButtonList()
{
    string[] buttons = { "A", "B", "Back", "BigButton", "etc..." }
    return buttons;
}

private void EchoButtons()
{
    for (int i = 0; i < ButtonList().Length; i++)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
        {
            // Echo the buttons
        }
    }
}

无论如何我可以使用数组中的字符串来定义按钮吗?示例(尽管这不起作用):

for (int i = 0; i < ButtonList().Length; i++)
{
    if (GamePad.GetState(PlayerIndex.One).Buttons.ButtonList()[i] == ButtonState.Pressed)
    {
        // Echo the buttons
    }
}

编辑:我希望这是有道理的,我不确定我是否解释得很好。

4

1 回答 1

2

您可以使用具有GamePadState作为参数的委托列表,并返回所需按钮的状态。

var getButtonState = new List<Func<GamePadState, ButtonState>>
{
    s => s.Buttons.A,
    s => s.Buttons.B,
    ...
};

// Example to get the state of the first item in the list.
ButtonState state = getButtonState[0]( GamePad.GetState( PlayerIndex.One ) );
于 2011-03-08T00:49:56.977 回答