Mouse input question
Posted: Tue Jun 30, 2020 11:58 pm
I'm fairly new to LOVE 2d, lua and coding in general. I've made some progress over the last few weeks, but the road ahead is a long one. What I'm stuck on at the moment is implementing a basic (I think) drag-and-drop code into my game (https://ebens.me/post/mouse-dragging-in-love2d) as a starting point. This example is for having LOVE draw an image and allow the mouse to grab and move the image. What I can't figure out is how to apply the code to an object that uses rxi's "Classic" code. I've put in Ebens' code to my object file and I'm not getting any errors (finally!), but I'm also not getting any input. Anything obvious jump out at anyone? Advice would be much appreciated. My code is below.
Code: Select all
RedBlocks = Object:extend(Object)
function RedBlocks:new(x, y, width, height)
self.image = love.graphics.newImage("red_block.png")
self.x = x
self.y = y
self.width = 60
self.height = 60
self.dragging = { active = false, diffX = 0, diffY = 0}
end
function RedBlocks:update(dt)
if self.dragging.active then
self.x = love.mouse.getX() - self.dragging.diffX
self.y = love.mouse.getY() - self.dragging.diffY
end
end
function RedBlocks:mousepressed(x, y, button)
if button == 1
and x > self.x and x < self.x + self.width
and y > self.y and y < self.y + self.height
then
self.dragging.active = true
self.dragging.diffX = x - self.x
self.dragging.diffY = y - self.y
end
end
function RedBlocks:mousereleased(x, y, button)
if button == 1 then
self.dragging.active = false
end
end
function RedBlocks:draw()
love.graphics.draw(self.image, self.x, self.y, 0, 2, 2)
end