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

Re: Mousepressed not working?

Post by xpali2 »

pedrosgali wrote: Wed Oct 17, 2018 9:13 am Did you even try drawing the rectangles to the screen?
Image
If you had you would have immediately seen your problem.
If you looked at my code you would see that is not the same as what I have. Plus you can see mousepressed isn't being called at all so the hitboxes aren't the problem. What it looks like since I have font objects and not normal text: https://ibb.co/hUgi0f
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

Re: Mousepressed not working?

Post by xpali2 »

KowalewskajA wrote: Tue Oct 16, 2018 1:14 am 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
Look I really appreciate that solution. I'll use it if all else fails. But I would really like to find out why love.mousepressed isn't being called at all if possible.
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 7:45 pm 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.
Thanks for your help. Where do I start to analyse why my mousepressed event isn't working? I am pretty clueless at this point.
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Mousepressed not working?

Post by zorg »

xpali2 wrote: Tue Oct 16, 2018 1:31 pm
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.
I'm skeptical about what you actually did here, or if you actually understood what pgimeno was explaining.
Maybe just share a love file with your current code, that way we could find the issue faster, instead of resorting to guessing.
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.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Mousepressed not working?

Post by pgimeno »

xpali2 wrote: Wed Oct 17, 2018 2:00 pm Thanks for your help. Where do I start to analyse why my mousepressed event isn't working? I am pretty clueless at this point.
You may have two mousepressed events.

You may be declaring it somewhere where it does not execute.

The print may be working but not being shown in the output for some reason.

Or many other possibilities that haven't occurred to me. As zorg notes, all we can do is guess, and I should add that it's frustrating.

One way to find out if it's being overwritten by another definition is to set a global immediately after declaring the love.mousepressed function:

Code: Select all

function love.mousepressed(...)
  ...
end
LoveMousepressed = love.mousepressed
and then in your love.draw, print both LoveMousepressed and love.mousepressed to see if they match.
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

Re: Mousepressed not working?

Post by xpali2 »

zorg wrote: Wed Oct 17, 2018 4:09 pm
xpali2 wrote: Tue Oct 16, 2018 1:31 pm
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.
I'm skeptical about what you actually did here, or if you actually understood what pgimeno was explaining.
Maybe just share a love file with your current code, that way we could find the issue faster, instead of resorting to guessing.
I did understand. And I changed it accordingly. Here is my main.lua file.

Code: Select all

love.graphics.setDefaultFilter("nearest", "nearest")

require("Icons&Extras/Menu/menu")

function love.load()
  gamestate = "inMenu"
  menu_load()
end

function love.draw()
  if gamestate == "inMenu" then
    menu_draw()
  end
end

function love.update(dt)
  if gamestate == "inMenu" then
  menu_update(dt)
  end
end

function love.mouspressed(x, y, button)
  print("Mousepressed was called")
if gamestate == "inMenu" then
  menu_mousepressed(x, y, button)
  end
end

function love.mousemoved(x, y)
  if gamestate == "inMenu" then
    menu_mousemoved(x, y)
    end
end
Hope it helps. I should add that the only extra code I have in this project is a conf.lua with nothing but the title, icon and window size
xpali2
Prole
Posts: 38
Joined: Thu Apr 05, 2018 4:47 pm

Re: Mousepressed not working?

Post by xpali2 »

pgimeno wrote: Wed Oct 17, 2018 4:37 pm
xpali2 wrote: Wed Oct 17, 2018 2:00 pm Thanks for your help. Where do I start to analyse why my mousepressed event isn't working? I am pretty clueless at this point.
You may have two mousepressed events.

You may be declaring it somewhere where it does not execute.

The print may be working but not being shown in the output for some reason.

Or many other possibilities that haven't occurred to me. As zorg notes, all we can do is guess, and I should add that it's frustrating.

One way to find out if it's being overwritten by another definition is to set a global immediately after declaring the love.mousepressed function:

Code: Select all

function love.mousepressed(...)
  ...
end
LoveMousepressed = love.mousepressed
and then in your love.draw, print both LoveMousepressed and love.mousepressed to see if they match.
I'll tell you it's just as frustrating on my end. If I am not giving you something you think you need to see to solve the issue then ask away, I am not an experienced coder so I can't guess what you need to hear. I have to say that I don't get what you mean with "two mousepressed events" if you could explain. I have checked the console many times by putting a print outside of the basic callbacks in main.lua that print to the console I am viewing: https://ibb.co/f6LEaf. I have tried "LoveMousepressed = love.mousepressed" but nothing changes, no errors or anything. Btw, mousemoved does work, so I reckon the mouse isn't the problem, meaning the mouse module is also not the issue.
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Mousepressed not working?

Post by pedrosgali »

Here's a .love of the file you posted with a main.lua to make it work, if you click in the boxes I drew the function gets called. I don't know what else to tell you.
menuhelp.love
I got it to launch with a console (windows only) to show that the function is being called.
I copied the code you posted and wrote a main.lua, I had to get rid of the fonts as I needed to run the file to check.

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 »

pedrosgali wrote: Wed Oct 17, 2018 6:18 pm Here's a .love of the file you posted with a main.lua to make it work, if you click in the boxes I drew the function gets called. I don't know what else to tell you.

menuhelp.love

I got it to launch with a console (windows only) to show that the function is being called.
I copied the code you posted and wrote a main.lua, I had to get rid of the fonts as I needed to run the file to check.
This one does work for me as well. It's a different version than my love but that doesn't seem to pose an issue. I have just as much of an idea of why this works and mine doesn't as you do.
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Mousepressed not working?

Post by pedrosgali »

The main.lua file should look like this:

Code: Select all

local menu = require "menu"

function love.load()
  menu_load()
end

function love.mousepressed(x, y, b)
  menu_mousepressed(x, y, b)
end

function love.update(dt)
  
end

function love.draw()
  menu_draw()
end
Well I'm sure there will be other things in there but that's the minimum you need to get your code working. Once you do your positioning is off :P You're drawing a box 400 wide with the text but checking a box 200 wide for clicks.

Code: Select all

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

Who is online

Users browsing this forum: Bing [Bot] and 25 guests