0

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

我的游戏光标通常偏向Windows鼠标的右侧,所以当我想将游戏光标移动到窗口左侧并单击鼠标左键时,会对游戏造成干扰,例如带一个应用程序在后台到顶部。

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

如您所见,游戏光标偏移到 Windows 光标的右侧,如果我使用游戏光标单击窗口左侧的某些内容,则后台应用程序(本例中为 Google Chrome)将被带到最前面,对比赛造成干扰。

我可以做些什么来不受任何干扰地使用我的游戏光标吗?

4

2 回答 2

0

通常,游戏中的光标会为您提供纹理,例如,[16,16] 处的像素是您“瞄准”的位置(例如,十字准线的中心)。您应该以鼠标为中心绘制它是使用 Mouse.GetState() 来获取位置,然后将鼠标纹理的绘制偏移“目标”点的“中心”的负值。

假设我们制作了一个自定义的 Mouse-Class:

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();
    }
}
于 2013-04-10T11:37:44.017 回答
0

我刚刚尝试将所有内容从他们的课程中移出,全部移到主游戏课程中。这解决了问题,但没有给我答案为什么会发生这种情况。

代码完全相同,只是组织成单独的类。

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

于 2013-04-10T02:36:00.253 回答