Mouse problems

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
User avatar
Chrisblue
Prole
Posts: 5
Joined: Sat Jan 12, 2013 1:10 pm

Mouse problems

Post by Chrisblue »

Hello everybody.

I just started to make a game with LÖVE Framework and already got some problems.
Everytime i try to use the love.mouse.isDown method or the mousepressed function nothing happens.
I tried different approaches and searched the forum for information but nothing helped so far.

These are my current codes:

Code: Select all

function love.update()
	mx = love.mouse.getX()
	my = love.mouse.getY()

	if love.mouse.isDown("l") then
			love.graphics.print(mx, 0, 0)
			love.graphics.print(my, 0, 20)
	end
end
It is a test if the method works well, but it seems like it is not working probably.

Code: Select all

function love.mousepressed(mx, my, button)
	mx = love.mouse.getX()
	my = love.mouse.getY()

	if button == "l" then
		if mx > (sc_width / 2 - 60) and mx < (sc_width / 2 + 60) and my > (sc_height / 2 - 40) and my < (sc_height / 2) then
			love.graphics.print("Button pressed!", 0, 0)
		end
	end
end
The second one is just a quick try for using a clickable button (The Coordinates are right).

I really hope somebody can help me. It looks like I just missed a small detail. :huh:
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Mouse problems

Post by micha »

Hi and welcome to löve.

All the drawing only works, if you put it into the love.draw function. In love.update they don't work (thats because the screen is cleared before love.draw is called.
So, just move the print function call into draw:

Code: Select all

function love.draw()
	if love.mouse.isDown("l") then
		love.graphics.print(mx, 0, 0)
		love.graphics.print(my, 0, 20)
	end
end
User avatar
Chrisblue
Prole
Posts: 5
Joined: Sat Jan 12, 2013 1:10 pm

Re: Mouse problems

Post by Chrisblue »

Thank you for your help. :awesome:

I already finished my working button class with hover effect:

Code: Select all

function GUI_button(text, x, y, event)
	if x < sc_width / 2 then a = 1 else a = -1 end
	x = x + 1;		y = y + 1;
	width = 120;	height = 40;
	textlength = string.len(text)*11 - 1;
	if mx > (x) and mx < (x + width) and my > (y) and my < y + height then
	love.graphics.setColor(77,57,0)
		if love.mouse.isDown("l") then
			action = event
		end
	else
	love.graphics.setColor(57,77,0)
	end
	love.graphics.rectangle("fill", x, y, width, height)
	love.graphics.rectangle("line", x - 1, y - 1, width + 2, height + 2)
	love.graphics.setColor(255,255,255)
	love.graphics.print(text, x + a*((width - textlength)/2), y + (height - 17)/2)
end
I am using the variable "action" as a number check in the Update function.
For each possible number, an action will be linked.

But I receive an error for the following lines of code :| :

Code: Select all

	if action = 1 then
		action = os.exit()
	end
It is the following error: Syntax error: main.lua:19: 'then' expected near '='
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Mouse problems

Post by Ref »

Chrisblue wrote:Thank you for your help. :awesome:


But I receive an error for the following lines of code :| :

Code: Select all

	if action = 1 then
		action = os.exit()
	end
It is the following error: Syntax error: main.lua:19: 'then' expected near '='
Try:

Code: Select all

if action == 1 then
action = love.event.push( 'quit' )
end
Last edited by Ref on Sat Jan 12, 2013 5:28 pm, edited 1 time in total.
User avatar
mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

Re: Mouse problems

Post by mickeyjm »

Chrisblue wrote:Thank you for your help. :awesome:

Code: Select all

	if action = 1 then
		action = os.exit()
	end
It should be

Code: Select all

	if action == 1 then
		action = os.exit()
	end
You need 2 equals signs in if statements and 1 in defining variables
Your screen is very zoomed in...
User avatar
Chrisblue
Prole
Posts: 5
Joined: Sat Jan 12, 2013 1:10 pm

Re: Mouse problems

Post by Chrisblue »

You need 2 equals signs in if statements and 1 in defining variables
Yes, that was the solution.
It seems like the greatest errors are always the simple ones.

Thank you for your help. Now I can start designing the Game GUI. ;)
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Mouse problems

Post by Roland_Yonaba »

Chrisblue wrote: But I receive an error for the following lines of code :| :

Code: Select all

	if action = 1 then
		action = os.exit()
	end
It is the following error: Syntax error: main.lua:19: 'then' expected near '='
Ref wrote: Try:

Code: Select all

action = love.event.push( 'quit' )
That won't solve the problem, Ref.
See , Chris, in Lua, the operator used for comparison is "==", while "=" is for assignment. See the use of Lua relational operators on PiL
Therefore, it should be:

Code: Select all

if action == 1 then action = os.exit() end
Also, you might want to consider that os.exit() is a Lua function that closes the host program. That would works flawlessly, but keep in mind that Love has its in-built dedicated commands, which are love.event.quit or love.event.push, passing string 'quit' as arg.

Note: Seems I've just been Mickeyjm'ed :crazy:
EDIT 2: Seems I'm late, here.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], zingo and 1 guest