You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MonoGame项目中瓦片(Tile)与MouseState更新延迟问题求助

MonoGame瓦片悬停高亮延迟问题解决方案

你贴出的单瓦片鼠标检测逻辑本身没有语法错误,3秒的延迟属于架构层面的问题,按以下优先级排查修复:

  • 首先检查Game类的时间步长配置:MonoGame默认开启固定时间步长,如果你误将TargetElapsedTime设置为秒级(比如TimeSpan.FromSeconds(3)),会导致Update逻辑每3秒才执行一次,渲染层虽然正常跑但高亮状态更新延迟。修改为默认配置即可:
// Game类构造/Initialize方法中修改
IsFixedTimeStep = true;
TargetElapsedTime = TimeSpan.FromTicks(166667); // 对应60次/秒更新
// 不需要固定步长也可以直接关闭
IsFixedTimeStep = false;
  • 优化鼠标状态获取逻辑:你当前在每个瓦片的Update里单独调用Mouse.GetState(),如果你的Tilemap瓦片数量超过1000,每帧上千次的鼠标状态读取会严重阻塞主线程,直接导致更新卡顿。改为全局统一读取一次鼠标状态,再传递给所有瓦片即可:
// Game类的Update方法中统一获取
protected override void Update(GameTime gameTime)
{
    _previousMouseGlobal = _currentMouseGlobal;
    _currentMouseGlobal = Mouse.GetState();
    // 遍历所有瓦片传递全局鼠标状态
    foreach (var tile in AllTiles)
    {
        tile.Update(gameTime, _currentMouseGlobal, _previousMouseGlobal);
    }
    base.Update(gameTime);
}

修改瓦片的Update方法,删除内部的鼠标状态读取逻辑:

public override void Update(GameTime gameTime, MouseState currentMouse, MouseState previousMouse)
{
    var mouseRectangle = new Rectangle(currentMouse.X, currentMouse.Y, 1, 1);
    _isHovering = false;
    if (mouseRectangle.Intersects(Rectangle))
    {
        _isHovering = true;
        if (currentMouse.LeftButton == ButtonState.Released && previousMouse.LeftButton == ButtonState.Pressed)
        {
            Click?.Invoke(this, new EventArgs());
        }
    }
}
  • 相机坐标转换排查:如果你做了相机平移、缩放功能,需要将鼠标的屏幕坐标转换为世界坐标再做碰撞检测,否则鼠标位置和瓦片碰撞盒不匹配会看起来像延迟:
// 全局Update中统一做坐标转换
Vector2 mouseWorldPos = Vector2.Transform(new Vector2(_currentMouseGlobal.X, _currentMouseGlobal.Y), Matrix.Invert(Camera.ViewMatrix));
Rectangle mouseWorldRect = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1);
// 传递转换后的世界坐标矩形给瓦片做碰撞检测即可
  • 可选性能优化:不用遍历所有瓦片做碰撞检测,直接用瓦片尺寸计算当前悬停的瓦片索引。比如你的瓦片是4x4像素,直接将鼠标世界坐标的X、Y分别除以4取整,就能直接拿到当前悬停瓦片的行列号,只更新对应瓦片的_isHovering状态即可,性能提升非常明显。

内容的提问来源于stack exchange,提问作者Sonny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 12:54:04