在 Love2D 中使用 Lua 播放声音时鼠标点击未检测到鼠标位置

Mouse position not detected on mouse click for playing sound with Lua in Love2D

我正在为视力不佳的人编写一个小游戏,但我很难获得鼠标位置。让我解释一下:

我需要知道鼠标光标在 table 中的位置,无需单击,然后我想播放声音。每个位置的声音都会不同。有什么想法吗?提前致谢!

例如,当鼠标在第一个框上时,将播放音频 "a1",当它在第二个框上时,将播放音频 "a2",依此类推。

我试过:

mouse_x, mouse_y = get_Position()

if mouse_x and mouse_y == map[x][y] then
if map[x][y] == 0.1 then
Audio:play()

但是它会循环并且声音会一直播放!

我认为部分问题与 love2d 的鼠标精度有关。

您很可能需要更改代码中的一些逻辑,使其更像

(有四种不同的场景,因为会分配1号房和2号房的顺序)

if map.x1 < mouse_x < map.x2 and map.y1 < mouse_y < map.y2 or
map.x1 > mouse_x > map.x2 and map.y1 > mouse_y > map.y2 or
map.x1 < mouse_x < map.x2 and map.y1 > mouse_y > map.y2 or
map.x1 > mouse_x > map.x2 and map.y1 < mouse_y < map.y2 then
     TEsound.play(soundList, "a1", 1, 0.1)
end

这是一张图片,解释了如何检测橡皮擦的鼠标是否与一条线重叠。

只有 2 个 x 和 y 坐标,上面的示例可能都太精确了,您可能必须通过在不等式的每一侧加减小数字来扩大鼠标到达的范围。

if (map.x1 - 2 < mouse_x and map.x2 + 2 > mouse_x and map.y1 - 2 < mouse_y and map.y2 + 2 > mouse_y)
            or (map.x1 + 2 > mouse_x and map.x2 - 2 < mouse_x and map.y1 + 2 > mouse_y and map.y2 - 2 < mouse_y)
            or (map.x1 - 2 < mouse_x and map.x2 + 2 > mouse_x and map.y1 + 2 > mouse_y and map.y2 - 2 < mouse_y)
            or (map.x1 + 2 > mouse_x and map.x2 - 2 < mouse_x and map.y1 - 2 < mouse_y and map.y2 + 2 > mouse_y)

或者另一种选择是使用 4 个 x 坐标和 4 个 y 坐标,假设您选择一个 2D 区域