如何在 XNA 中使用 DirectX.DirectInput
Joystick.cs
using System;
using Microsoft.DirectX.DirectInput;
namespace gameproject
{
/// <summary>
/// Description of device.
/// </summary>
class joysticks
{
public static Device joystick;
public static JoystickState state;
public static void InitDevices() //Function of initialize device
{
//create joystick device.
foreach (DeviceInstance di in Manager.GetDevices(
DeviceClass.GameControl,
EnumDevicesFlags.AttachedOnly))
{
joystick = new Device(di.InstanceGuid);
break;
}
if (joystick == null)
{
//Throw exception if joystick not found.
}
//Set joystick axis ranges.
else {
foreach (DeviceObjectInstance doi in joystick.Objects)
{
if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
{
joystick.Properties.SetRange(
ParameterHow.ById,
doi.ObjectId,
new InputRange(-5000, 5000));
}
}
joystick.Properties.AxisModeAbsolute = true;
joystick.SetCooperativeLevel(null,CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);
//Acquire devices for capturing.
joystick.Acquire();
state = joystick.CurrentJoystickState;
}
}
public static void UpdateJoystick() // Capturing from device joystick
{
//Get Joystick State.
if(joystick!=null)
state = joystick.CurrentJoystickState;
}
}
}
在这一行中,发生了错误,
joystick.SetCooperativeLevel(null,CooperativeLevelFlags.NonExclusive
| CooperativeLevelFlags.Background);
错误,
Error 1 The type 'System.Windows.Forms.Control' is defined in an
assembly that is not referenced.
You must add a reference to assembly 'System.Windows.Forms...
我正在处理 XNA 3.0 和 .NET 3.5,那么该错误意味着什么?
joystick.cs
using System;
using Microsoft.DirectX.DirectInput;
namespace gameproject
{
/// <summary>
/// Description of device.
/// </summary>
class joysticks
{
public static Device joystick;
public static JoystickState state;
public static void InitDevices() //Function of initialize device
{
//create joystick device.
foreach (DeviceInstance di in Manager.GetDevices(
DeviceClass.GameControl,
EnumDevicesFlags.AttachedOnly))
{
joystick = new Device(di.InstanceGuid);
break;
}
if (joystick == null)
{
//Throw exception if joystick not found.
}
//Set joystick axis ranges.
else {
foreach (DeviceObjectInstance doi in joystick.Objects)
{
if ((doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
{
joystick.Properties.SetRange(
ParameterHow.ById,
doi.ObjectId,
new InputRange(-5000, 5000));
}
}
joystick.Properties.AxisModeAbsolute = true;
joystick.SetCooperativeLevel(null,CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background);
//Acquire devices for capturing.
joystick.Acquire();
state = joystick.CurrentJoystickState;
}
}
public static void UpdateJoystick() // Capturing from device joystick
{
//Get Joystick State.
if(joystick!=null)
state = joystick.CurrentJoystickState;
}
}
}
In this line, an error occurred,
joystick.SetCooperativeLevel(null,CooperativeLevelFlags.NonExclusive
| CooperativeLevelFlags.Background);
error,
Error 1 The type 'System.Windows.Forms.Control' is defined in an
assembly that is not referenced.
You must add a reference to assembly 'System.Windows.Forms...
I'm working on,XNA 3.0 and .NET 3.5, so what means that error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SetCooperativeLevel
将 System.Windows.Forms.Control 对象作为第一个参数(其中为 null),因此您仍应引用应用程序中定义此类的程序集。从您的应用程序/游戏中添加对 System.Windows.Forms.dll 的引用,然后尝试。如果您正在使用的代码正在使用一些您没有在后台引用的其他类,那没关系,但是当它们是公共的(就像它们是参数或从您调用的方法返回)时,您必须引用其中的程序集这些类型已定义。类似的 stackoverflow 帖子:
调试错误“类型“xx”在未引用的程序集中定义”
SetCooperativeLevel
takesSystem.Windows.Forms.Control
object as a first parameter (where you have null), so you should still reference assembly where this class is defined in your application. Add reference do System.Windows.Forms.dll from your app/game and try then. If code you are using is using some other classes that you haven't referenced under the hood, it's ok, but when they are public (like they are parameter or are returned from methods you are calling), you have to reference assemblies in which those types are defined.Similar stackoverflow post:
Debugging error "The Type 'xx' is defined in an assembly that is not referenced"