使用XNA访问Xbox 360游戏杆(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/using-xna-to-access-an-xbox-360-joystick-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2995 个词 阅读量 0使用XNA访问Xbox 360游戏杆(译文)
原文地址:https://www.codeproject.com/Articles/16983/Using-XNA-to-Access-an-Xbox-360-Joystick
原文作者:Joel Ivory Johnson
译文由本站 robot-v1.0 翻译
前言
This article demonstrates how to use the Microsoft XNA Framework to read the state of an Xbox 360 controller
本文演示了如何使用Microsoft XNA Framework读取Xbox 360控制器的状态
- 下载源文件-13.74 KB(Download source files - 13.74 KB)
- 下载演示项目-216.95 KB(Download demo project - 216.95 KB)
介绍(Introduction)
随着XNA的发布,Microsoft提供了一个在PC和Xbox 360上编写游戏的平台.与受管理的DirectX相比,XNA提供了对硬件的简化访问. XNA框架也完全支持Xbox 360控制器.在本文中,我将使用Visual Studio 2005和XNA Framework创建一个演示表单,该表单将显示连接到系统的Xbox 360控制器的完整状态.(With the release of XNA, Microsoft has made available a platform for writing games on the PC and for the Xbox 360. XNA provides more simplified access to hardware than managed DirectX did. The Xbox 360 controllers are also fully supported by the XNA Framework. In this article, I’ll be using Visual Studio 2005 and the XNA Framework to create a demonstration form that will display the full state of Xbox 360 controllers connected to the system.) 2012年11月11日更新(Update 11 November 2012)-我发布了另一篇文章,该文章使用XInput而不是Xna来访问相同的功能. XInput可以访问其他信息(例如电池信息),但是其他文章中的代码版本需要Windows8.您可以找到该文章.(- I’ve posted another article that makes use of XInput instead of Xna to access the same functionality. XInput gives access to additional information (such as information on the battery) but the version of the code in the other articles requires Windows 8. You can find the article) 这里(here) .(.)
先决条件(Prerequisites)
此代码示例使用XNA Framework中的程序集,您将需要安装XNA可再发行文件才能运行示例程序.您可以从下载(This code example uses assemblies from the XNA Framework and you will need to install the XNA redistributables to run the example program. You can download it from) 这里(here) .在创建自己的XNA项目时,请记住添加一个程序集引用(. In creating your own XNA project, remember to add an assembly reference to) Microsoft.Xna.Framework
和(and) Microsoft.Xna.Framework.Input
.(.)
XNA(XNA)
XNA Framework基于.NET Compact Framework的子集,并设计为在Windows XP SP2上运行.在撰写本文时,尽管Framework确实在这些系统上运行,但Windows 2003和Vista尚未正式受支持. Xbox 360具有一个XNA游戏加载器,该加载器为基于XNA的游戏提供了运行时环境.(The XNA Framework is based on a subset of the .NET Compact Framework and was designed to run on Windows XP SP2. At the time of this writing, Windows 2003 and Vista are not officially supported, although the Framework does operate on these systems. The Xbox 360 has an XNA game loader which provides a runtime environment for XNA based games.)
由于XNA基于.NET Compact Framework的子集,因此不支持Windows Forms命名空间或网络命名空间.但是,如果您仅针对Windows平台,则可以使用(Since XNA is based on a subset of the .NET Compact Framework, there is no support for the Windows Forms namespace or the network namespace. However, if you are targeting the Windows platform only, you can use) System.Windows.Forms
和(and) System.Net
以及其他需要的名称空间.(and other namespaces as needed.)
输入类型(Types of Inputs)
控制器上的输入为模拟或数字输入.数字输入将仅具有以下两种状态之一,以指示是否已按下它们:(The inputs on the controller are either analog or digital. The digital inputs will only have one of two states to indicate whether or not they are being pressed:) ButtonState.Pressed
要么(or) ButtonState.Released
.控制器上的数字输入是标有A,B,X,Y,LB,RB,返回,开始,指点杆下的按钮和D-Pad的按钮.(. The digital inputs on the controller are the buttons labelled A, B, X, Y, LB , RB, Back, Start, the buttons under both of the thumb sticks, and the D-Pad.)
模拟输入的状态用浮点数表示.控制器上的触发器将导致返回0.0(未按下触发器)和1.0(完全按下触发器)之间的值.两个指尖杆针对x和y轴返回的值在-1.0到+1.0之间,其中0是该轴的中心点.(The state of the analog inputs is represented with a floating point number. The triggers on the controller will cause values between 0.0 (the trigger is not pressed) and 1.0 (the trigger is pressed all the way down) to be returned. The two thumb sticks return values between -1.0 and +1.0 for the x and y axis, where 0 is the center point for the axis.)
Xbox 360控制器中心的按钮不能访问.它仅由Xbox操作系统使用.(The button in the center of the Xbox 360 controller is not accessible. It’s only used by the Xbox operating system.)
访问控制器(Accessing the Controller)
控制器的状态通过(The state of the controller is accessed through the) Microsoft.XNA.Framework..Input.GamePad
类.此类仅具有三种方法(除了从(class. This class only has three methods (beyond those inherited from) System.Object
).这些方法是(). Those methods are) GetCapabilities
,(,) GetState
和(, and) SetVibration
.对于此演示,我将仅使用(. For this demonstration I will only use) GetState
和(and) SetVibration
.这些方法的第一个参数是(. These methods take for their first argument a) PlayerIndex
值.这用于指定要与之交互的控制器.(value. This is used to specify the controller with which we will interact.)
的(The) GetState
方法返回一个(method returns a) GamePadState
结构.(struct.) GamePadState
在名为几个子结构中包含控制器的状态(contains the state of the controller in several sub-structs named) Buttons
,(,) DPad
,(,) IsConnected
,(,) ThumbSticks
和(, and) Triggers
.的(. The) Button
和(and) DPad
结构具有以控制器上的10个按钮和面板上的四个方向命名的成员.(structs have members named after the 10 buttons on the controller and the four directions on the) DPad
每个将被设置为(that will each be set to) ButtonState.Pressed
和(and) ButtonState.Released
(在((there is also an enumeration in the) System.Windows.Forms namespace
,如果您使用的是(, you may need to disambiguate between the two if you are using the) System.Windows.Forms
与(with) Microsoft.Xna.Framework.Input
).().)
//Setting or clearing a checkbox depending on the state of a button on the controller.
this.gamePadState = GamePad.GetState(this.playerIndex);
this.buttonA.Checked = (this.gamePadState.Buttons.A == Input.ButtonState.Pressed);
的(The) Triggers
结构包含成员(struct contains the members) Left
和(and) Right
带有浮点的数字表示触发器被按下的距离.(that have floating point to represent how far down the trigger is pressed.)
的(The) ThumbSticks
成员有两个成员(member has two members) Left
和(and) Right
.这些每个都是一个(. Each one of these is a) Vector2
,或一组指示操纵杆位置的浮点X和Y值.一种(, or a group of floating point X and Y values indicating the position of the joystick. A) Vector
值(X:0,Y:0)表示操纵杆在中间位置,(X:-1,Y:0)表示操纵杆在最左侧位置,(X:0,Y:1)表示正在按下控制器,依此类推.请注意,您将永远不会同时看到X和Y值处于最极端的状态.(with the values (X:0,Y:0) mean the joystick is in the center position, (X:-1,Y:0) means the joystick is to the far left position, (X:0, Y:1) means the controller is being pressed up, and so on. Note that you will never see the X and Y values in their most extreme state at the same time.)
两个振动电机(The Two Vibration Motors)
Xbox 360控制器有两个用于振动的电动机.左电机会引起缓慢的振动.正确的电动机会引起快速振动.可以通过以下方式打开电动机(The Xbox 360 controllers have two motors for vibration. The left motor causes a slow vibration. The right motor causes a quick vibration. The motors can be turned on with the) SetVibration
方法.此方法需要(method. This method takes a) PlayerIndex
以及两个介于0.0f和1.0f之间的浮点数,以指示每个电动机应振动的强度.(and two floating point numbers between 0.0f and 1.0f to indicate how strong each motor should vibrate.)
//Turning the slow vibration motor on to 50% maximum
GamePad.SetVibration(playerIndex, 0.5f, 0.0f);
关于计划(About the Program)
该示例程序以固定的时间间隔连续轮询Xbox 360控制器并更新屏幕状态.(The example program continually polls the Xbox 360 controller on a timed interval and updates the onscreen state.) Checkbox
es用于数字输入.模拟输入用进度条表示.拇指杆的轴在最负位置为空,在中心位置为半满,在最正位置为满.(es are used for the digital inputs. The analog inputs are represented with progress bars. The axis for the thumb sticks will be empty in the most negative position, half full in the center position, and full in the most positive position.)
您也可以使用UI底部的数字上/下输入来打开电动机.我只允许电动机在有限的时间内打开,并在程序终止时将其关闭.(You can also turn the motors on using the numeric up/down inputs at the bottom of the UI. I’ve only allowed the motors to be on for a limited time and also turn them off when the program is terminating.)
结论(Conclusion)
您会发现XNA Framework大大简化了与Xbox 360控制器的交互.在下一篇文章中,我将展示如何使用XNA和XNA输入设备来控制屏幕上的对象来渲染图形.(You’ll find that the XNA Framework greatly simplifies interaction with the Xbox 360 controllers. In my next article, I’ll show how to render graphics using XNA and using an XNA input device to control onscreen objects.)
历史(History)
- 2006年12月20日-文章已创建(20th December, 2006 - Article created)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# C#2.0 WinXP Win2003 Windows .NET2.0 VS2005 Visual-Studio VS.NET2003 Dev 新闻 翻译