Xna 处理游戏光标,而不是windows光标
早些时候,我遇到了一个问题,我的Windows光标与游戏不协调,并在这里问我如何解决这个问题。一位成员建议我隐藏Windows光标并创建一个自定义游戏光标,所以我这样做了。然而,出现了一个新问题 我的游戏光标通常偏移到Windows鼠标的右侧,因此当我想将游戏光标移动到窗口的左侧并单击鼠标左键时,会对游戏造成干扰,例如将背景中的应用程序置于顶部 以下是我的意思: 如您所见,游戏光标偏移到Windows光标的右侧,如果我使用游戏光标单击窗口左侧的某个对象,则背景中的应用程序(本例中为Google Chrome)将被带到前面,从而对游戏造成干扰Xna 处理游戏光标,而不是windows光标,xna,xna-4.0,Xna,Xna 4.0,早些时候,我遇到了一个问题,我的Windows光标与游戏不协调,并在这里问我如何解决这个问题。一位成员建议我隐藏Windows光标并创建一个自定义游戏光标,所以我这样做了。然而,出现了一个新问题 我的游戏光标通常偏移到Windows鼠标的右侧,因此当我想将游戏光标移动到窗口的左侧并单击鼠标左键时,会对游戏造成干扰,例如将背景中的应用程序置于顶部 以下是我的意思: 如您所见,游戏光标偏移到Windows光标的右侧,如果我使用游戏光标单击窗口左侧的某个对象,则背景中的应用程序(本例中为Google
我能做些什么来不受干扰地使用我的游戏光标吗?我刚刚尝试将所有内容移出他们的类,全部转移到主游戏类中。 这解决了问题,但没有给我一个为什么会发生这种情况的答案 代码完全相同,只是被组织成不同的类 那么,有人知道这是为什么吗?
为什么使用面向对象编程而不是将所有东西都放在游戏类中会打乱我的鼠标协调和其他功能?通常,游戏光标会有一个纹理,例如,[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();
}
}