2011-03-08 94 views
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 
    } 
} 

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

回答

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)); 
相关问题