如何用字符串定义结构?

时间:2011-03-08 00:41:44

标签: c# string xna-4.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
    }
}

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

1 个答案:

答案 0 :(得分: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 ) );
相关问题