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 » Sat Feb 24, 2018 9:12 pm

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 9 times
main.lua
Main code
(2.5 KiB) Downloaded 9 times

MrFariator
Citizen
Posts: 79
Joined: Wed Oct 05, 2016 11:53 am

Re: Two buttons don't work!

Post by MrFariator » Sat Feb 24, 2018 10:27 pm

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.
-- Run once in a blue moon
if math.random() > 0.99 then
  local account = Twitter:getProfile ( https://twitter.com/GravityCircuit )
  account:post ( currentProgress:getGIF() )
end

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests