如何在 XNA 中使用 DirectX.DirectInput

发布于 2024-10-31 11:12:28 字数 2327 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如狂蝶 2024-11-07 11:12:28

SetCooperativeLevel 将 System.Windows.Forms.Control 对象作为第一个参数(其中为 null),因此您仍应引用应用程序中定义此类的程序集。从您的应用程序/游戏中添加对 System.Windows.Forms.dll 的引用,然后尝试。如果您正在使用的代码正在使用一些您没有在后台引用的其他类,那没关系,但是当它们是公共的(就像它们是参数或从您调用的方法返回)时,您必须引用其中的程序集这些类型已定义。

类似的 stackoverflow 帖子:
调试错误“类型“xx”在未引用的程序集中定义”

SetCooperativeLevel takes System.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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文