Skip to content

Instantly share code, notes, and snippets.

Created September 6, 2011 00:13
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mebens/1196228 to your computer and use it in GitHub Desktop.
Save mebens/1196228 to your computer and use it in GitHub Desktop.
Complete code for my tutorial on mouse dragging in Love2D.
function love.load()
rect = {
x = 100,
y = 100,
width = 100,
height = 100,
dragging = { active = false, diffX = 0, diffY = 0 }
function love.update(dt)
if then
rect.x = love.mouse.getX() - rect.dragging.diffX
rect.y = love.mouse.getY() - rect.dragging.diffY
function love.draw()"fill", rect.x, rect.y, rect.width, rect.height)
function love.mousepressed(x, y, button)
if button == "l"
and x > rect.x and x < rect.x + rect.width
and y > rect.y and y < rect.y + rect.height
then = true
rect.dragging.diffX = x - rect.x
rect.dragging.diffY = y - rect.y
function love.mousereleased(x, y, button)
if button == "l" then = false end
Copy link

ghost commented Jun 12, 2017

You might wanna use the updated version. Available since LÖVE 0.10.0

Copy link

Why don't you use the love.mousemoved (x, y, dx, dy, istouch) function ?

Copy link

Kilozen commented Aug 3, 2022

Note to anyone using this after Love 0.10.0

The current callbacks use numbers, rather than letter strings to identify the mouse buttons.
So lines that say:
button == "l" -- (lower case 'L')
have to be changed to say:
button == 1 -- (button number 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment