Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在单局游戏中,鼠标位置是偏移的_C#_Monogame - Fatal编程技术网

C# 在单局游戏中,鼠标位置是偏移的

C# 在单局游戏中,鼠标位置是偏移的,c#,monogame,C#,Monogame,所以我在我的游戏中实现了射击,但是当我调整窗口大小或移动(因为我的相机)时,鼠标位置会发生偏移,而且射击显然不会击中我想要的位置,我不知道如何修复它。以下是相关代码: Shot.cs public class Shot : Sprite { private float Speed = 500; private Vector2 Dir; private Vector2 Velocity; public Shot(Texture2D texture, Vector

所以我在我的游戏中实现了射击,但是当我调整窗口大小或移动(因为我的相机)时,鼠标位置会发生偏移,而且射击显然不会击中我想要的位置,我不知道如何修复它。以下是相关代码:

Shot.cs

public class Shot : Sprite {

    private float Speed = 500;
    private Vector2 Dir;
    private Vector2 Velocity;

    public Shot(Texture2D texture, Vector2 position)
        : base(texture) {
        this.Position = position;
        this.Dir = Input.GetMousePos() - Position;
    }

    void Start() {

    }

    public void Update(GameTime gameTime) {
        var delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

        Dir.Normalize();
        Velocity = Dir * Speed;
        Position += Velocity * delta;
    }
}
Camera.cs GetTransform()(请注意,这不是我写的)

spriteBatch.Begin()

还想补充一点,这不是一个特定于拍摄的问题,当我尝试拖动GUI窗口时,位置也会偏移,因此我认为我需要修改我的GetMousePosition方法,但我不确定如何修改

编辑:这里是GetMousePos()


如前所述,您可以添加
GetMousePos
方法吗?谢谢,我添加了它。我在谷歌上做了更多的搜索,并使用以下答案实现了它:
public Matrix GetTransform(GraphicsDevice graphicsDevice) {
    transform = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
                                         Matrix.CreateRotationZ(Rotation) *
                                         Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                                         Matrix.CreateTranslation(new Vector3(graphicsDevice.Viewport.Width * 0.5f, graphicsDevice.Viewport.Height * 0.5f, 1));
    return transform;
}
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null, camera.GetTransform(GraphicsDevice));
public static Vector2 GetMousePos() {
    return new Vector2(mouseState.X, mouseState.Y);
}