2011-04-09 48 views
1

joystick.cs如何在XNA使用DirectX.DirectInput

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,那麼錯誤是什麼意思?

回答

2

SetCooperativeLevel需要將System.Windows.Forms.Control對象作爲第一個參數(其中您有null),因此您仍然應該在應用程序中定義此類的位置時引用程序集。從你的應用/遊戲添加引用做System.Windows.Forms.dll,然後嘗試。如果你正在使用的代碼是使用其他一些你沒有在引用下引用的類,那就沒問題,但是當它們是公共的時候(比如它們是參數或者是你調用的方法返回的),你必須引用程序集這些類型被定義。

類似的堆棧溢出帖子: Debugging error "The Type 'xx' is defined in an assembly that is not referenced"