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

查看:47
本文介绍了在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)

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

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天全站免登陆