2013-04-09 81 views
0

早些时候,我遇到了一个问题,我的Windows光标与游戏不协调,并在此问我如何解决此问题。一位成员建议我隐藏Windows光标并创建一个自定义游标光标,所以我这样做了。但是,出现了一个新问题。处理游戏光标,而不是Windows光标

我的游标光标通常偏移到Windows鼠标的右侧,所以当我想将游标光标移动到窗口的左侧并单击我的鼠标左键时,它会引起游戏干扰,例如因为将背景中的应用带到顶端。

这里是我的意思图片:http://i.imgur.com/nChwToh.png

正如你可以看到,游戏光标偏移到Windows光标的权利,如果我使用的游戏鼠标点击的东西左侧窗口中的应用程序(本例中为Google Chrome)将被置于前台,从而对游戏造成干扰。

有什么我可以做的使用我的游戏光标没有任何干扰?

回答

0

我刚刚试图将所有内容都移出课程,全部进入主游戏类。 这解决了这个问题,但并没有给我一个答案,为什么会发生这种情况。

代码是完全一样的,它只是组织成分离类。

那么,有没有人知道这是为什么? 为什么使用面向对象的编程,而不是把所有东西都放在游戏类中,搞乱了我的鼠标协调和东西?

0

通常情况下,你会为​​游戏中的游标指定一个纹理,例如[16,16]处的像素就是你瞄准的地方(比如十字线的中心)。你以鼠标为中心绘制这个图像的方法是使用Mouse.GetState()来获取位置,然后用“目标”点的“中心”的负值偏移鼠标纹理的图形。

让我们说,我们创建一个自定义鼠标类别:

public class GameMouse 
{ 
    public Vector2 Position = Vector2.Zero; 
    private Texture2D Texture { get; set; } 
    private Vector2 CenterPoint = Vector2.Zero; 
    public MouseState State { get; set; } 
    public MouseState PreviousState { get; set; } 

    //Returns true if left button is pressed (true as long as you hold button) 
    public Boolean LeftDown 
    { 
     get { return State.LeftButton == ButtonState.Pressed; } 
    } 

    //Returns true if left button has been pressed since last update (only once per click) 
    public Boolean LeftPressed 
    { 
     get { return (State.LeftButton == ButtonState.Pressed) && 
      (PreviousState.LeftButton == ButtonState.Released); } 
    } 

    //Initialize texture and states. 
    public GameMouse(Texture2D texture, Vector2 centerPoint) 
    { 
     Texture = texture; 
     CenterPoint = centerPoint; 
     State = Mouse.GetState(); 

     //Calling Update will set previousstate and update Position. 
     Update(); 
    } 

    public void Update() 
    { 
     PreviousState = State; 
     State = Mouse.GetState(); 
     Position.X = State.X; 
     Position.Y = State.Y; 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Begin(); 
     spriteBatch.Draw(Texture, Position - CenterPoint, Color.White); 
     spriteBatch.End(); 
    } 
}