How can i define a structure with a string?
I have
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
}
}开发者_JAVA技巧
}
Is there anyway i could use the string from the array to define the button? Example (although this does't work):
for (int i = 0; i < ButtonList().Length; i++)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.ButtonList()[i] == ButtonState.Pressed)
{
// Echo the buttons
}
}
Edit: i hope this makes sense, i wasn't sure i explained it well.
You can use a list of delegates which have GamePadState
as a parameter, and return the state for the desired buttons.
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 ) );
精彩评论