Skip to content

Instantly share code, notes, and snippets.

@nathanharper
Last active April 25, 2017 19:16
Show Gist options
  • Save nathanharper/6254760 to your computer and use it in GitHub Desktop.
Save nathanharper/6254760 to your computer and use it in GitHub Desktop.
Really basic example of a draggable shape in the Love2D framework
local box = {x=10,y=10,w=50,h=80}
local mouseButton = 1
function love.load()
love.graphics.setBackgroundColor(255,255,255)
love.graphics.setColor(225,62,62)
end
function love.draw()
love.graphics.rectangle("fill", box.x, box.y, box.w, box.h)
end
function love.mousepressed(x, y, button, istouch)
if button ~= mouseButton then return end
local dx = x - box.x
local dy = y - box.y
if dx >= 0 and dx <= box.w and dy >= 0 and dy <= box.h then
love.update = function()
if not love.mouse.isDown(mouseButton) then
love.update = nil
return
end
local x,y = love.mouse.getPosition()
box.x = x - dx
box.y = y - dy
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment