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: 70
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Mouse dragging

Post by Pospos » Fri Jun 15, 2018 9:54 pm

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 28 times
ⵜⴰⵎⴰⵣⵉⵖⵜ(https://en.wikipedia.org/wiki/Berbers)

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Mouse dragging

Post by davisdude » Sat Jun 16, 2018 1:47 am

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: 1379
Joined: Sun Oct 18, 2015 2:58 pm

Re: Mouse dragging

Post by pgimeno » Sat Jun 16, 2018 5:32 am

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
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories - portland (mobile orientation)
The MS-Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

User avatar
Pospos
Citizen
Posts: 70
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Re: Mouse dragging

Post by Pospos » Sat Jun 16, 2018 9:29 am

Thanks, now it works.
ⵜⴰⵎⴰⵣⵉⵖⵜ(https://en.wikipedia.org/wiki/Berbers)

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests