## Mousepressed not working?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

### Mousepressed not working?

Hi all,

I'm trying to make a menu screen. I seem to be doing something wrong in the mousepressed function since it isn't calling the function when the screen is clicked. Can anybody help me figure out why? I also have some code that makes the buttons red if you hold the mouse over them but I haven't finished it so don't mind it.

Thanks,

Code: Select all

function menu_load()
titlefont = love.graphics.newFont("Icons&Extras/Font/slkscr.ttf", 64)
buttonfont = love.graphics.newFont("Icons&Extras/Font/slkscr.ttf", 48)
startButton = {text = "Start", x = 200, y = 300, call = startgame},
exitButton = {text = "Exit", x = 200, y = 500, call = exitgame},
optionsButton = {text = "Options", x = 200, y = 400, call = openoptions}
}
end

love.graphics.setFont(titlefont)
love.graphics.printf("KNIGHTLY", 200, 100, 400, "center")

love.graphics.setFont(buttonfont)
for i, v in pairs(menubuttons) do
love.graphics.printf(v.text, v.x, v.y, 400, "center")
end
end

end

for i, v in pairs(menubuttons) do
if x > v.x and x < v.x + 200 and y > v.y and y < v.y + 35 then
v.call()
end
end
end

for i, v in pairs(menubuttons) do
if x > v.x and x < v.x + 400 and y > v.y and y < v.y + 35 then
buttonColour = love.graphics.setColor(255, 0, 0)
else
buttonColour = love.graphics.setColor(255, 255, 255)
end
end
end

function startgame()
print("startinggame")
end

function exitgame()
print("quiting")
end

function openoptions()
print("fetchingoptions")
end

KowalewskajA
Prole
Posts: 3
Joined: Mon Oct 15, 2018 11:10 pm
Location: Berlin/Germany
Contact:

### Re: Mousepressed not working?

Before I offer you a solution I would definately recommend you to read the documentation or try some tutorial .

Anyway here is a working solution(rectangles are just for visualing the hitboxes):

Code: Select all

function love.load()
end

function love.update(dt)
local  	down = love.mouse.isDown(1)
local 	mx = love.mouse.getX()
local	my = love.mouse.getY()

end

function love.draw()
end

startButton = 	{hover = false, text = "Start", 	x = 200, y = 300, call = startgame},
exitButton = 	{hover = false, text = "Exit", 		x = 200, y = 500, call = exitgame},
optionsButton = {hover = false, text = "Options", 	x = 200, y = 400, call = openoptions}
}
redColor = {255,0,0,255}
whtColor = {255,255,255,255}
end

love.graphics.printf("KNIGHTLY", 200, 100, 400, "center")
for i, v in pairs(menubuttons) do
if v.hover == true then
love.graphics.setColor(redColor)
else
love.graphics.setColor(whtColor)
end
love.graphics.printf(v.text, v.x, v.y, 400, "center")
love.graphics.rectangle('line', v.x + 200 - 100, v.y - 20, 200, 40)
love.graphics.setColor(whtColor)
end
end

end

for i, v in pairs(menubuttons) do
if mx > v.x + 200 - 100 and mx < v.x + 200 + 100 and my > v.y - 20 and my < v.y + 20 then
v.hover=true
if down == true then v.call() end
else
v.hover=false
end
end
end

function startgame()
print("startinggame")
end

function exitgame()
print("quiting")
end

function openoptions()
print("fetchingoptions")
end

Thats how it looked afterwards:

Hope that helped ya,
KowalewskajA

xNick1
Party member
Posts: 260
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

### Re: Mousepressed not working?

Are you calling the menu_mousepressed function from main.lua?
Like:

Code: Select all

function love.mousepressed( x, y, button, istouch, presses )
end

Othewise how would it know it has to call it?

If so, are you sure it isn't calling it?
Maybe the coordinates are wrong.
Try to print something in the function, like:

Code: Select all

function menu_mousepressed(x, y, button)
print("I entered the menu mousepressed function")
for i, v in pairs(menubuttons) do
if x > v.x and x < v.x + 200 and y > v.y and y < v.y + 35 then
v.call()
end
end
end

Currently working on a lucid dreaming adventure called Aarstider: http://bit.ly/2mXmgPI

pedrosgali
Party member
Posts: 105
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

### Re: Mousepressed not working?

Your positioning is off, draw a rectangle round the box in your draw function like this

Code: Select all

function menu_draw()
--love.graphics.setFont(titlefont)
love.graphics.printf("KNIGHTLY", 200, 100, 400, "center")

--love.graphics.setFont(buttonfont)
for i, v in pairs(menubuttons) do
--A rectangle here will show you the bounds of your collision check.
love.graphics.rectangle("line", v.x, v.y, 200, 35)
love.graphics.printf(v.text, v.x, v.y, 400, "center")
end
end
--This happened because here you check a box 200 wide and 35 high but above you centered your text in a box 400 wide.
for i, v in pairs(menubuttons) do
if x > v.x and x < v.x + 200 and y > v.y and y < v.y + 35 then
v.call()
end
end
end



Code: Select all

if not wearTheseGlasses() then
chewing_on_trashcan = true
end

xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

### Re: Mousepressed not working?

xNick1 wrote:
Tue Oct 16, 2018 7:06 am
Are you calling the menu_mousepressed function from main.lua?
Like:

Code: Select all

function love.mousepressed( x, y, button, istouch, presses )
end

Othewise how would it know it has to call it?

If so, are you sure it isn't calling it?
Maybe the coordinates are wrong.
Try to print something in the function, like:

Code: Select all

function menu_mousepressed(x, y, button)
print("I entered the menu mousepressed function")
for i, v in pairs(menubuttons) do
if x > v.x and x < v.x + 200 and y > v.y and y < v.y + 35 then
v.call()
end
end
end

I had not actually done this, but putting this function in the main.lua - to my surprise - did not actually fix the issue. When I put a print statement in the function to see if a click was detected at all it doesn't print anything. In fact, it even doesn't print anything when I put the print function in love.mousepressed itself.
Last edited by xpali2 on Tue Oct 16, 2018 1:44 pm, edited 1 time in total.

xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

### Re: Mousepressed not working?

pedrosgali wrote:
Tue Oct 16, 2018 7:21 am
Your positioning is off, draw a rectangle round the box in your draw function like this

Code: Select all

function menu_draw()
--love.graphics.setFont(titlefont)
love.graphics.printf("KNIGHTLY", 200, 100, 400, "center")

--love.graphics.setFont(buttonfont)
for i, v in pairs(menubuttons) do
--A rectangle here will show you the bounds of your collision check.
love.graphics.rectangle("line", v.x, v.y, 200, 35)
love.graphics.printf(v.text, v.x, v.y, 400, "center")
end
end
--This happened because here you check a box 200 wide and 35 high but above you centered your text in a box 400 wide.
for i, v in pairs(menubuttons) do
if x > v.x and x < v.x + 200 and y > v.y and y < v.y + 35 then
v.call()
end
end
end


But x is set at 200 and the line is wrapped at 400 so then the text is in a 200 pixel window isn't it? Or I am completely misunderstanding how the wrapping of printf works. Either way, changing it to 400 pixels still doesn't resolve the issue. Also telling me the function isn't being called somehow.

pgimeno
Party member
Posts: 1531
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Mousepressed not working?

xpali2 wrote:
Tue Oct 16, 2018 1:31 pm
In fact, it even doesn't print anything when I put the print function in love.mousepressed itself.
Are you sure you're monitoring the console window instead of the game window? If you're under Windows, are you using lovec.exe?
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories - portland (mobile orientation)
The MS-Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

### Re: Mousepressed not working?

pgimeno wrote:
Tue Oct 16, 2018 3:00 pm
xpali2 wrote:
Tue Oct 16, 2018 1:31 pm
In fact, it even doesn't print anything when I put the print function in love.mousepressed itself.
Are you sure you're monitoring the console window instead of the game window? If you're under Windows, are you using lovec.exe?
I'm using zerobrane studio and the console window that belong to that. It worked previously and I haven't changed anything, so there is no reason why it shouldn't print to that console now I reckon.

pgimeno
Party member
Posts: 1531
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Mousepressed not working?

Well, it's hard to help any further without having something we can look into by ourselves. The code in the OP definitely can't help diagnosing why your love.mousepressed event isn't working.
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories - portland (mobile orientation)
The MS-Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

pedrosgali
Party member
Posts: 105
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

### Re: Mousepressed not working?

Did you even try drawing the rectangles to the screen?

if not wearTheseGlasses() then
end