Page 1 of 1

Getting mouse position and playing a sound

Posted: Wed Dec 05, 2018 3:29 pm
by NewtonEinherjar
0
down vote
favorite
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!

I tried with:

Code: Select all

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 where the sound keeps playing and playing!

Re: Getting mouse position and playing a sound

Posted: Sun Dec 09, 2018 1:13 am
by Ross
Please post the rest of your code, or a .love file if the project is small. There could be many different things going wrong with the code you posted, depending on what the rest of it is.

Are you getting errors in the console?

How are you checking for clicks? Is this code inside the love.mousepressed function?

In your first line you are using the comparison operator == instead of setting the variables with =. I would think that by itself would just crash your game.

In the next line, I don't know what `x` and `y` are, but I don't think this will do what you want it to. It really means this:

Code: Select all

if (mouse_x ~= nil) and (mouse_y == map[x][y]) then
	-- do stuff...
I don't know what `get_Position()` does either, but if it always returns two numbers, then your first condition (if mouse_x) will always be true, so that's not doing anything.

Re: Getting mouse position and playing a sound

Posted: Sun Dec 09, 2018 1:15 am
by Ross
Oh, sorry I misread, you said -without- having a click. How do you want it to work then? You want to continuously play different sounds as the mouse moves around?

Re: Getting mouse position and playing a sound

Posted: Sun Dec 09, 2018 2:54 am
by zorg
I think he wants to have each sound play once when the mouse cursor enters a specific cell.

First off, love.mouse.getPosition returns pixel-positions, so you'd need to either convert those into your own table grid's coordinates, or code it like it's a simple axis aligned bounding box collision system; either way, after that's done, you'll need to store the previous cell's coordinates, and if the cursor's position went into a new cell, play a sound once, then update the previous coordinates to the new cell's coordinates.

Also, you need to compare x and y separately, "x and y == blah" will never work the way you'd want it to.