如何在XNA中使用DirectX.DirectInput

时间:2011-04-09 10:36:47

标签: c# xna directx joystick xna-3.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,那么这意味着什么错误?

1 个答案:

答案 0 :(得分:2)

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

类似stackoverflow帖子: Debugging error "The Type 'xx' is defined in an assembly that is not referenced"