【问题标题】:Mouse position not detected on mouse click for playing sound with Lua in Love2D在 Love2D 中使用 Lua 播放声音时未检测到鼠标位置
【发布时间】:2019-05-07 01:35:53
【问题描述】:

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

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

例如,当鼠标在第一个盒子上时会播放音频“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
  • this 你在找什么?
  • @csaar,我试过了,但没用。我用 getPosition() 得到了更好的结果,但它进入了一个循环。我在原帖中添加了一些信息,请看一下。
  • 我不知道,如果你知道你真正在那里做什么。首先第 1 行不符合语法。应该是mouse_x, mouse_y = get_Position()== 是一个条件操作数。您的第一个条件评估,如果mouse_x 具有任何值,并且如果mouse_y 具有与map[x][y] 相同的值。这是你的意图吗?我会建议调试变量和 get_position() 的值。

标签: lua love2d


【解决方案1】:

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

【讨论】:

  • @Newton 如果这对您没有帮助,请发表评论,我会尽力解释更多
猜你喜欢
  • 2015-03-03
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多