在 Love2D 中使用 Lua 播放声音时,鼠标单击时未检测到鼠标位置 [英] Mouse position not detected on mouse click for playing sound with Lua in Love2D

查看:23
本文介绍了在 Love2D 中使用 Lua 播放声音时,鼠标单击时未检测到鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a little game for visual deficient people, but I'm having a hard time getting the mouse position. Let me explain :

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

I need to know where in the table the mouse cursor is, without having a click, and then I want to play a sound. That sound would be different for every position. Any thoughts? Thanks, in advance!

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

e.g., when the mouse is on the 1st box would be played the audio "a1", when it's on the 2nd box, "a2", and so on.

我尝试过:

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

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

But it makes a loop and the sound keeps playing forever!

推荐答案

我认为部分问题与 love2d 使用鼠标的精确度有关.

I think part of the problem relates to how precise love2d is able to be with it's mouse.

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

You'll most likely have to change some of the logic in your code around to be more like

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

(There's four different scenario's because of the ordering in house 1 and 2 would be assigned)

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

下面的图片解释了如何检测橡皮擦的鼠标是否与一条线重叠.

Here's an image that explains detecting if the mouse for an eraser is overlapping with a line.

只有 2 个 x 和 y 坐标,上面的例子可能都太精确了,你可能不得不通过在不等式的每一边加上和减去小数字来扩大鼠标到达的范围.

With only 2 x and y coordinates, that example above will probably all be too precise, and you may have to expand the range that the mouse reaches by adding and subtracting small numbers on each side of the inequality.

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 区域

Or another option would be to use 4 x coordinate's and 4 y coordinates, assuming your selecting a 2D area

这篇关于在 Love2D 中使用 Lua 播放声音时,鼠标单击时未检测到鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆