2011-03-08 95 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)); 
相關問題