Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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# 纹理2D坐标未显示在正确位置_C#_Monogame - Fatal编程技术网

C# 纹理2D坐标未显示在正确位置

C# 纹理2D坐标未显示在正确位置,c#,monogame,C#,Monogame,我一直在使用SpriteBatch.Draw()渲染一些自定义“按钮”。然而,今天我意识到,我应该绘制按钮的坐标与我的鼠标声称的它们被渲染的坐标不一致。这是一个问题,因为如果图形渲染关闭,则很难检测是否正在单击按钮。另一种可能是鼠标坐标已关闭。怎么了 如果您需要更多信息,请询问。谢谢大家! 硬编码按钮位置: /// <summary> /// The x position at which the left part of the buttons on the mai

我一直在使用SpriteBatch.Draw()渲染一些自定义“按钮”。然而,今天我意识到,我应该绘制按钮的坐标与我的鼠标声称的它们被渲染的坐标不一致。这是一个问题,因为如果图形渲染关闭,则很难检测是否正在单击按钮。另一种可能是鼠标坐标已关闭。怎么了

如果您需要更多信息,请询问。谢谢大家!

硬编码按钮位置:

    /// <summary>
    /// The x position at which the left part of the buttons on the main menu begin.
    /// </summary>
    public static readonly int ButtonX = 20;

    /// <summary>
    /// How wide the buttons are.
    /// </summary>
    public static readonly int ButtonWidth = 200;

    /// <summary>
    /// How tall the buttons are.
    /// </summary>
    public static readonly int ButtonHeight = 50;



    /// <summary>
    /// The y position of the top of the new game button.
    /// </summary>
    public static readonly int NewGameButtonY = 100;

    /// <summary>
    /// The y position of the top of the new game button.
    /// </summary>
    public static readonly int QuitButtonY = 200;
按钮的呈现方式:

    private static void DrawButton(MonoButton button, ref SpriteBatch spBatch)
    {
        if (button.Visible)
        {
            spBatch.Draw(button.Image, button.DrawingBounds, colorMask);
            RenderingPipe.DrawString(MainMenuLayout.MainMenuFont, button.Text, button.DrawingBounds, Alignment.Center, colorMask, ref spBatch);
        }
    }
视觉显示某事物的关闭程度:

新游戏按钮的左上角应该是(20100)

编辑:我的笔记本电脑的本机分辨率是1920x1080,但游戏的显示分辨率肯定低于全屏模式


编辑#2:我后来意识到,虽然鼠标偏移可以工作,但简单地将monogame窗口分辨率设置为监视器的本机分辨率要容易得多。这完全解决了这个问题,没有鼠标偏移

鼠标将坐标报告到光标的中心,而不是指针的尖端

理解为什么,认为一个人的鼠标是另一个人的触摸屏。 您可以对按钮的点击框应用轻微偏移,但最好只偏移从GetState获得的坐标,并将其封装到属性中:

int offsetX = 3, offsetY = 2;
MouseState _currentState; // assume this is set by main game loop every call to Update()
int MousePositionX => _currentState.X + offsetX;
int MousePositionY => _currentState.Y + offsetY;
确切的偏移量可能取决于多种因素,包括现有按钮组件中的意外坐标变换或偏移(请检查边界框计算),因此可能需要反复试验才能立即找到解决方案。如果需要,请确保在不同分辨率、DPI和HID(人机界面设备,即鼠标、触摸屏、游戏板输入)下进行测试


如果确实需要动态计算光标图标,您可以查看查询环境以获取有关光标图标(如果有的话)的信息的方法。毕竟,指针并不是人们所知道的唯一使用的游标

ButtonX=20,NewGameButtonY=100,也许你的意思是左上角应该是(100,20)?@Gusman据我所知,坐标不是这样工作的。它们应该是(x,y)。然后它不是左上角,而是左上角…我认为首先要知道什么是关闭的,按钮还是鼠标,这很重要?你也能展示一下这个按钮在游戏中的坐标吗?@Gusman X在右边是正的,Y在底部是正的。对不起,我以前不理解你的评论。假设这是一个问题,是否有一个通用的偏移量可以解决这个问题,或者我必须做一些尝试和错误?你可以查询系统的光标的细节-我将添加作为一个编辑
int offsetX = 3, offsetY = 2;
MouseState _currentState; // assume this is set by main game loop every call to Update()
int MousePositionX => _currentState.X + offsetX;
int MousePositionY => _currentState.Y + offsetY;