Lua在Love2D中使用鼠标点击无法检测到鼠标位置

我正在为视觉有障碍的人编写一款小游戏,但是我很难获取鼠标位置。我需要知道鼠标指针在表格中的位置,而不需要点击任何东西,然后我想播放一段声音。每个位置的声音都不同。有什么想法吗?先谢谢了! 例如,当鼠标在第一个框上时,会播放音频“a1”,当鼠标在第二个框上时,会播放“a2”,依此类推。

我尝试使用:

mouse_x,mouse_y=get_Position()

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

但是它会产生一个循环,并且声音会一直播放!

点赞
用户6331353
用户6331353

我认为部分问题与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

这是一个图像,它解释了如何检测鼠标是否与线条重叠以用于橡皮擦。

enter image description here

仅使用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区域。

2018-12-10 05:01:59