Page 1 of 1

Mouse problems

Posted: Sat Jan 12, 2013 1:32 pm
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:

Re: Mouse problems

Posted: Sat Jan 12, 2013 3:18 pm
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

Re: Mouse problems

Posted: Sat Jan 12, 2013 3:42 pm
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 '='

Re: Mouse problems

Posted: Sat Jan 12, 2013 3:54 pm
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

Re: Mouse problems

Posted: Sat Jan 12, 2013 4:06 pm
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

Re: Mouse problems

Posted: Sat Jan 12, 2013 4:14 pm
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. ;)

Re: Mouse problems

Posted: Sat Jan 12, 2013 4:16 pm
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.