How to use DirectX.DirectInput in XNA

走远了吗. 提交于 2019-12-20 05:15:40

问题


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?


回答1:


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"



来源:https://stackoverflow.com/questions/5604492/how-to-use-directx-directinput-in-xna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!