Issues of A drag 'n' drop using tables and for loops
Posted: Wed Mar 29, 2023 4:52 pm
Hi, I'm having a small problem with the Drag 'n' drop system that when I hover the mouse over another rectangle, they automatically join, I don't know how to solve it, because if I put a variable that is activated when I hover the mouse over it, it activates drag and drop, activates and the two join again in the same way, can you help me my code is this:
Help me pls
Code: Select all
-- create some rectangles
local rects = {}
local numRects = 15
for i = 1, numRects do
rects[i] = {
x = math.random(0, love.graphics.getWidth()),
y = math.random(0, love.graphics.getHeight()),
w = 50,
h = 50,
}
end
function love.update(dt)
if love.mouse.isDown(1) then -- if left mouse button is pressed
-- get mouse position
local mx, my = love.mouse.getPosition()
-- check if mouse is over any rectangle
for i = 1, numRects do
local rect = rects[i]
if mx > rect.x and mx < rect.x + rect.w and my > rect.y and my < rect.y + rect.h then
-- if mouse is over rectangle, then update position
rect.x = mx - rect.w/2
rect.y = my - rect.h/2
end
end
end
end
function love.draw()
for i = 1, numRects do
local rect = rects[i]
love.graphics.rectangle("fill", rect.x, rect.y, rect.w, rect.h)
end
end