Two buttons don't 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.
Post Reply
BatteryJellyfish
Prole
Posts: 1
Joined: Sat Feb 24, 2018 8:00 pm

Two buttons don't work!

Post by BatteryJellyfish »

I am new to coding in Love2d and Lua. I have a start screen where you can "Start" or "More".
Both buttons don't work though. Only the one coded lower... Here is the snippet of code where
the buttons are:

Code: Select all

function love.mousepressed(x, y, button, isTouch)
  if button == 1 and gameState == 1  and y < 510 and y > 419 and x < 320 and x > 99 then
    gameState = 2
  end
end

function love.mousepressed(x, y, button, isTouch)
  if button == 1 and gameState == 1  and y < 510 and y > 419 and x < 740 and x > 559 then
    gameState = 3
  end
end
Let me know how I can get both buttons to work if it is possible...
Attachments
Game.love
Love file
(560 KiB) Downloaded 97 times
main.lua
Main code
(2.5 KiB) Downloaded 90 times
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Two buttons don't work!

Post by MrFariator »

What you're doing there is redefining the love.mousepressed function, meaning that the first will never be used. Think of it like you're assigning a value to a variable, and then immediately afterwards assigning another value to it. What you need to do instead is combine the two:

Code: Select all

function love.mousepressed(x, y, button, isTouch)
  if button == 1 and gameState == 1  and y < 510 and y > 419 and x < 320 and x > 99 then
    gameState = 2
  elseif button == 1 and gameState == 1  and y < 510 and y > 419 and x < 740 and x > 559 then
    gameState = 3
  end
end
Other point worth noting (beside making the if statements neater) is that you probably should check the click position by percentage, rather than absolute pixel coordinates. This is because if the player were to resize the game window, the pixel positions might not line up correctly, depending on how the graphics are drawn.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 52 guests