Reputation: 33
I am trying to make it so that if a user clicks the left mouse button at 200x300 then a variable will be changed. I searched online for tutorials but cannot find a solution.
function love.load()
medium = love.graphics.newFont(45)
small = love.graphics.newFont(25)
micro = love.graphics.newFont(14)
StartGame = false
end
function love.draw()
love.graphics.setColor(255, 0, 0)
love.graphics.setFont(small)
love.graphics.print("Play", 200, 300)
end
function love.mousepressed(x, y, button)
if love.mousepressed( 200, 300, 'l' ) then
StartGame = true
end
end
function love.mousereleased(x, y, button)
end
function love.quit()
end
Upvotes: 2
Views: 3517
Reputation: 1
Self explaining, the other examples had l
as in L
, that is wrong! the return is 1 2 or 3
testvar = "PLAY"
location = "location"
function love.load()
medium = love.graphics.newFont(45)
small = love.graphics.newFont(25)
micro = love.graphics.newFont(14)
StartGame = false
end
function love.upate()
if StartGame == true then love.quit()
end
end
function love.draw()
love.graphics.setColor(255, 0, 0)
love.graphics.setFont(small)
love.graphics.print(testvar, 200, 300)
love.graphics.print(location, 100, 100)
end
function love.mousepressed(x, y, button)
testvar = button
if button == 1 then location = "x:"..tostring(x).." y:"..tostring(x) --extra nested if block is not nessary this can be evaluated in the same if block...
if (inbox(200, 300, 20, x, y)) then
testvar = "StartGame"
-- StartGame = true
end
end
end
function inbox(cx, cy, scale, x, y)
local dx = cx + scale --love draws from left top corner,
local dy = cy + scale --i assume you want scale to be the same size as your object
if (x>cx and x<dx) and (y>cy and y<dy) then
return true
end
return false
end
function love.mousereleased(x, y, button)
end
function love.quit()
end
Upvotes: 0
Reputation: 67
to select a single point perfectly use of the below code
function love.mousepressed(x, y, button)
if button == 1 and x>200 and x<230 and y>280 and y<320 then
StartGame = true
end
end
Upvotes: 1
Reputation: 6251
This will set the variable when the user clicks down at 200, 300
function love.mousepressed(x, y, button)
if button == "l" and x == 200 and y == 300 then
StarGame = true
end
end
But that is likely to be too demanding on the user to select a single point perfectly. So the code below adds 10 pixels around the point (200, 300) to make it easier to click.
local function inCircle(cx, cy, radius, x, y)
local dx = cx - x
local dy = cy - y
return dx * dx + dy * dy <= radius * radius
end
function love.mousepressed(x, y, button)
if button == "l" and inCircle(200, 300, 10, x, y) then
StartGame = true
end
end
Try changing 10
to find what feels right.
Upvotes: 4