瓷砖和 MouseState MonoGame 的更新时间很慢

Posted

技术标签:

【中文标题】瓷砖和 MouseState MonoGame 的更新时间很慢【英文标题】:Slow update time with tiles and MouseState MonoGame 【发布时间】:2021-12-16 09:41:27 【问题描述】:

这里是非常新的开发人员。

在我的程序中,我有一个使用单纯形噪声库随机生成的世界地图。最重要的是,我正在尝试绘制一个透明 4x4 瓷砖的瓷砖地图,当鼠标悬停在一个瓷砖上时,这些瓷砖看起来略微半透明。

我已经完成了这项工作,但突出显示的图块需要大约 3 秒才能更新到鼠标的当前位置。有什么办法可以解决这个问题吗?

这是我在 tile 类中进行 MouseState 检查的代码:

public override void Update(GameTime gameTime)

    _previousMouse = _currentMouse;
    _currentMouse = Mouse.GetState();

    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());
        
    

抱歉,如果格式错误或问得不好,请先发帖,所以仍然掌握一切:)

【问题讨论】:

4x4 网格是否覆盖了屏幕上的一个矩形区域? @Strom 渲染时是的 【参考方案1】:

反转逻辑:

不要用鼠标检查数千个图块对象,而是将鼠标应用于单个对象。

假设您有一个 tile 对象的列表或数组:

添加一个新对象以检查鼠标悬停并单击:

public class MouseDetect(Tile[] tiles) // replace with List<> as needed

    int PrevHover = -1; // used to unHover

    // if (Area == Screen) make the next two lines `const`, so the compiler will remove all uses...
    int AreaX = 0; //Area x offset
    int AreaY = 0; //Area y offset

    int AreaW = 800; //Area width
    int AreaH = 480; //Area height
    const int Grid = 4; // assumes square
    const int GridW = AreaW / Grid;


// I Will assume the `Delegate Click` in `Tile` is  public
    public void Update(MouseState ms, MouseState oms), //_currentMouse = ms and _previousMouse = oms;
    
        int mouseIndex = (ms.X - AreaX) % Gridw + (ms.Y - AreaY) / GridW;
        tiles[PrevHover].Hover = false;
        PrevHover = mouseIndex;
        tiles[PrevHover].Hover = true;

        //Check Release
        if(tiles[PrevHover].Hover && ms.LeftButton == ms.ButtonState.Released && oms.LeftButton == ButtonState.Pressed)
         tiles[PrevHover].Click(tiles[PrevHover], new EventArgs());
    

从 Tile 类中删除更新。


以后阅读本文的任何人的注意事项:

切勿在每个步骤中多次调用Mouse.GetState();

预定义或框架名称(例如 Rectangle)绝不应用作标识符。

即重命名并更正为CollRectangle

if (CollRectangle.Contains(ms.Position))

【讨论】:

以上是关于瓷砖和 MouseState MonoGame 的更新时间很慢的主要内容,如果未能解决你的问题,请参考以下文章

Monogame - 音乐和音效不能同时播放

XNA (MonoGame) 世界空间和物体运动

iOS 8 和 Monogame 的方向错误?

如何在monogame中缩小和增长一个矩形?

在monogame中克隆和绘制对象

使用C#和MonoGame开发俄罗斯方块游戏