Mouse dragging

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Mouse dragging

Post 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?
Attachments
dragging.love
(406 Bytes) Downloaded 274 times
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Mouse dragging

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Mouse dragging

Post 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
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Re: Mouse dragging

Post by Pospos »

Thanks, now it works.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests