2012-06-25 76 views
0

我想通過速度而不是通過絕對位置來設置鼠標對象。 (雖然它跟蹤兩者,但速度是我想要更新它的方式。)C#XNA鼠標移動速度?

但是我遇到了一些問題。

首先,在窗口模式下,鼠標不能正確操作。因爲一旦它脫離屏幕鼠標假髮了。我嘗試使用Mouse.SetPosition每Update()打勾將其設置爲屏幕的中心,將其鎖定到窗口。但是這也會導致問題。

在行動中看到剛剛編譯這裏的.sln問題 - >https://github.com/redcodefinal/Clixel

一切都是自動的,因此所有你需要做的只是編譯。 ClxG已經有一個ClxMouse對象。 (ClxG.Mouse)

這是我正在使用的整個Mouse類。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace org.clixel 
{ 
    public class ClxMouse : ClxSprite 
    { 
     private MouseState _curmouse, _lastmouse; 

     private int _scrollwheel; 


     public bool LeftDown 
     { 
      get 
      { 
       if (_curmouse.LeftButton == ButtonState.Pressed) 
        return true; 
       else 
        return false; 
      } 
      set { } 
     } 

     public bool RightDown 
     { 
      get 
      { 
       if (_curmouse.RightButton == ButtonState.Pressed) 
        return true; 
       else 
        return false; 
      } 
      set { } 
     } 

     public bool MiddleDown 
     { 
      get 
      { 
       if (_curmouse.MiddleButton == ButtonState.Pressed) 
        return true; 
       else 
        return false; 
      } 
      set { } 
     } 

     public bool LeftPressed 
     { 
      get 
      { 
       if (_curmouse.LeftButton == ButtonState.Pressed && _lastmouse.LeftButton == ButtonState.Released) 
        return true; 
       else 
        return false; 
      } 
      set { } 
     } 

     public bool RightPressed 
     { 
      get 
      { 
       if (_curmouse.RightButton == ButtonState.Pressed && _lastmouse.RightButton == ButtonState.Released) 
        return true; 
       else 
        return false; 
      } 
      set { } 
     } 

     public bool MiddlePressed 
     { 
      get 
      { 
       if (_curmouse.MiddleButton == ButtonState.Pressed && _lastmouse.MiddleButton == ButtonState.Released) 
        return true; 
       else 
        return false; 
      } 
      set { } 
     } 

     public MouseState CurMouse 
     { 
      get 
      { 
       return _curmouse; 
      } 
      set { } 
     } 

     public MouseState LastMouse 
     { 
      get 
      { 
       return _lastmouse; 
      } 
      set { } 
     } 

     public ClxMouse() 
      : base(ClxAssets.Textures.Cursor) 
     { 
      _curmouse = Mouse.GetState(); 
      _lastmouse = _curmouse; 
      CollisionBox = new Rectangle(ClxG.Screen.X/2, ClxG.Screen.Y/2, Texture.Width, Texture.Height); 
      this.Solid = false; 
      Mouse.SetPosition(CollisionBox.X, CollisionBox.Y); 
     } 

     public ClxMouse(Texture2D _texture) 
      : base(_texture) 
     { 
      _curmouse = Mouse.GetState(); 
      _lastmouse = _curmouse; 
      CollisionBox = new Rectangle(ClxG.Screen.Center.X, ClxG.Screen.Center.Y, Texture.Width, Texture.Height); 
     } 

     public override void Update() 
     { 
      _lastmouse = _curmouse; 
      _curmouse = Mouse.GetState(); 

      Velocity = new Vector2(_curmouse.X - _lastmouse.X, _curmouse.Y - _lastmouse.Y); 
      Console.WriteLine(Velocity.ToString()); 
      base.Update(); 
     } 
    } 
} 

TLDR:如何在不爆炸整個世界的情況下將鼠標鎖定到窗口?

我真的很感激任何幫助。

回答

1

你可以檢查你的鼠標是否超出每幀的範圍。如果是,則需要將其設置到窗口的另一側。您需要在屏幕內部創建一些邊距來檢查邊界是否已經太晚,如果鼠標實際上不在屏幕上)。因此,如果您的屏幕尺寸爲800x600,則可以使用邊距(因此鼠標需要停留在760x560的盒子中,偏移量爲(20,20)),並且在特定的框架中,鼠標位置爲(400,567),您知道它超出了(0,7)[鼠標位置 - 邊距] - >如果x和/或y爲正值,則需要使用這些值重置鼠標位置。所以在這個例子中,新的鼠標位置將是(400,27)[27 cuz在每個邊緣上都有20px的邊緣](x保持不變,因爲400-760將小於0)。這樣您就可以將鼠標移動到無窮大,並使用每幀的鼠標位置差異來計算速度。