love.mousepressed how does it work?

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.
Fatmat927
Prole
Posts: 42
Joined: Wed Sep 18, 2013 1:15 am

Re: love.mousepressed how does it work?

Post 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
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: love.mousepressed how does it work?

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: love.mousepressed how does it work?

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests