How is the Xbox 360 Button Alias ​​in C #?

I am new to C Sharp and am writing a game with the XNA Framework.

I'm trying to set variables for buttons on an XBox 360 controller, so I can reconfigure the game functions of the buttons in one place and not change the direct links to buttons everywhere.

So, if I want to assign an attack button, instead:

if (gamePadState.IsButtonDown(Buttons.B)
{
   // do game logic
}

I want to do this:

if (gamePadState.IsButtonDown(MyAttackButton)
{
   // do game logic
}

Any ideas? I am sure this is a very simple solution, but I tried several approaches and no one has worked yet. Thank!

+3
source share
3 answers

Buttons is just an enumeration, so you just need to create a variable with a name like

Buttons MyAttackButton = Buttons.B;
+5
source

An alternative would be to define an enum somewhere:

public enum MyButtons
{
    AttackButton = Buttons.B,
    DefendButton = Buttons.A
}

, :

if (gamePadState.IsButtonDown((Buttons)MyButtons.DefendButton))
+2

:

enum MyButtons { ShootButton, JumpButton }

Dictionary<MyButtons, Buttons> inputMap = new Dictionary<MyButtons, Buttons>()
{
    { MyButtons.ShootButton, Buttons.Y },
    { MyButtons.JumpButton,  Buttons.B },
}

...

if (gamePadState.IsButtonDown(inputMap[MyButtons.ShootButton]))
{
    // Shoot...
}

, , .

+1
source

Source: https://habr.com/ru/post/1777001/


All Articles