简体   繁体   English

如何在C#中使用Xbox 360按钮?

[英]How to alias Xbox 360 buttons in C#?

I'm new to C Sharp, and writing a game w/ the XNA Framework. 我是C Sharp的新手,并且正在编写一个带有XNA Framework的游戏。

I'm trying to establish variables for the buttons on the XBox 360 controller, so I can reconfigure the buttons' game functions in one place and not have to change direct references to the buttons everywhere. 我正在尝试为XBox 360控制器上的按钮建立变量,因此我可以在一个地方重新配置按钮的游戏功能,而不必在任何地方更改对按钮的直接引用。

So if I want to assign a button to "attack", instead of this: 所以,如果我想指定一个“攻击”按钮,而不是:

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

I want to do this: 我想做这个:

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

Any ideas? 有任何想法吗? I'm sure it's a very simple solution, but I've tried several approaches and none have worked yet. 我确信这是一个非常简单的解决方案,但我已经尝试了几种方法,但还没有一种方法可行。 Thanks! 谢谢!

Buttons只是一个枚举,所以你只需要创建一个具有该名称的变量

Buttons MyAttackButton = Buttons.B;

An alternative would be to define an enum somewhere: 另一种方法是在某处定义枚举:

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

Then to test it: 然后测试一下:

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

You could also create a dictionary: 你也可以创建一个字典:

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...
}

The advantage of this method is that the button map can be modified at runtime, so you can use it to implement customizable control settings. 此方法的优点是可以在运行时修改按钮映射,因此您可以使用它来实现可自定义的控件设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM