Page 1 of 1
Mouse dragging
Posted: Fri Jun 15, 2018 9:54 pm
by Pospos
Hi, i wanted to create mouse dragging for a project of mine called dogboxing.
but, i keep having issues, like this problem, when the mouse is stuck with the dragged thing
can you help me?
Re: Mouse dragging
Posted: Sat Jun 16, 2018 1:47 am
by davisdude
Your problem is that you never check if the mouse is released, so dactive is always true. Consider adding an else statement to the love.mouse.isdown check to set dactive to false.
Re: Mouse dragging
Posted: Sat Jun 16, 2018 5:32 am
by pgimeno
I think the issue is a confusion about when to do what.
You need to capture the coordinate difference only when the mouse button goes down, not every frame while it's pressed. The easy way is to use the love.mousepressed event for that.
Code: Select all
function love.mousepressed(mx, my, b)
if b == 1 then
diffx = mx - x
diffy = my - y
end
end
Then, while the mouse button is pressed, you move the shape.
Code: Select all
function love.update(dt)
if love.mouse.isDown(1) then
mx, my = love.mouse.getPosition()
x = mx - diffx
y = my - diffy
end
end
Re: Mouse dragging
Posted: Sat Jun 16, 2018 9:29 am
by Pospos
Thanks, now it works.