Page 2 of 2

Re: love.mousepressed how does it work?

Posted: Mon Sep 23, 2013 12:19 am
by Fatmat927
okay now my button works but when i press it it puts error in my mousepressed function saying trying to compare number to nil? can u help me?

Code: Select all

function love.mousepressed(mouseX, mouseY, l)
	local mouseX, mouseY = love.mouse.getPosition ()
    if l_mouseX >= WindowWidth / 2 - 75 and mouseX <= WindowWidth / 2 + 75 and mouseY >= WindowHeight / 2 - 103 and mouseY <= WindowHeight / 2 - 50 then
	require = game.lua
	end
end

Re: love.mousepressed how does it work?

Posted: Mon Sep 23, 2013 4:53 am
by raidho36
There's so much wrong with this little piece of code that I hardly know where do I begin. Let's go line by line.

First, you re-define your mouse coordinates for some reason, that's excessive and will generally break the event since mouse may have been in another position when the event was triggered.

Second, you have some l_mouseX variable which I suppose is undefined (hence nil math error).

Third, you have Window* variables which supposedly hold the window dimensions, they might be nil as well.

Fourth, you assign a variable "game.lua" (or shall I type it as "game[ 'lua' ]" ) to the "require" variable, which for starters break the require functionality and then the "game.lua" variable probably doesn't exist anyway.

Try reading the manual before trying to actually do something you have no idea how to work with.

Re: love.mousepressed how does it work?

Posted: Mon Sep 23, 2013 6:35 am
by Robin
So, as to change it so that it may work:

Code: Select all

function love.mousepressed(mouseX, mouseY, l)
    if mouseX >= WindowWidth / 2 - 75 and mouseX <= WindowWidth / 2 + 75 and mouseY >= WindowHeight / 2 - 103 and mouseY <= WindowHeight / 2 - 50 then
        require('game')
    end
end
Note that this is a bad way of doing things. You'd usually load all the modules up front, and then later you just flick a switch as to what state is active. I think you need to learn about tables.