Getting mouse position and playing a sound

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
NewtonEinherjar
Prole
Posts: 1
Joined: Wed Dec 05, 2018 3:22 pm

Getting mouse position and playing a sound

Post 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!
Ross
Citizen
Posts: 97
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Getting mouse position and playing a sound

Post 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.
Ross
Citizen
Posts: 97
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Getting mouse position and playing a sound

Post 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?
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Getting mouse position and playing a sound

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests