Mousepressed not working?

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.
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

Mousepressed not working?

Post by xpali2 »

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)
  menubuttons = {
    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

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 
  love.graphics.printf(v.text, v.x, v.y, 400, "center")
  end
end

function menu_update(Somethinglikedt)
  
end

function menu_mousepressed(x, y, button)
  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

function menu_mousemoved(x, y)
   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
User avatar
KowalewskajA
Prole
Posts: 3
Joined: Mon Oct 15, 2018 11:10 pm
Location: Berlin/Germany
Contact:

Re: Mousepressed not working?

Post by KowalewskajA »

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

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

end

function love.draw()
	menu_draw()
end

function menu_load()
	menubuttons = {
	    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

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

function menu_update(Somethinglikedt)
  
end

function menu_mousehandling(mx, my, down)
  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:
Image

Hope that helped ya,
KowalewskajA
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Mousepressed not working?

Post by xNick1 »

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

Code: Select all

function love.mousepressed( x, y, button, istouch, presses )
    menu_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
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Mousepressed not working?

Post by pedrosgali »

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.
function menu_mousepressed(x, y, button)
  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?

Post by xpali2 »

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 )
    menu_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?

Post by xpali2 »

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.
function menu_mousepressed(x, y, button)
  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.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Mousepressed not working?

Post by pgimeno »

xpali2 wrote: Tue Oct 16, 2018 1:31 pmIn 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?
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

Re: Mousepressed not working?

Post by xpali2 »

pgimeno wrote: Tue Oct 16, 2018 3:00 pm
xpali2 wrote: Tue Oct 16, 2018 1:31 pmIn 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.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Mousepressed not working?

Post by pgimeno »

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.
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Mousepressed not working?

Post by pedrosgali »

Did you even try drawing the rectangles to the screen?
Image
If you had you would have immediately seen your problem.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 61 guests