herrinbleu wrote: ↑Fri Apr 07, 2017 1:17 pm
At the moment, I have several objects overlapping each other and when I click and drag, overlapping objects are all moved.
-- Code was here...
How should I make it such that only the top object is moved?
And also, how can I allow objects clicked to be moved to the top of the z order?
Hi and welcome to the forums!
First, let's assume that your objects table is already z-sorted (1==top, #objects==bottom), in which case you only need to do the following small edit:
Code: Select all
function dragging_detect(x, y, button)
for i,v in ipairs(objects) do
if x > v.x and x < v.x + v.width
and y > v.y and y < v.y + v.height then
v.dragging.active = true
v.dragging.diffX = x - v.x
v.dragging.diffY = y - v.y
-- In lua, the below code works, you don't need any temporary variable to store one of them.
objects[1], objects[i] = objects[i], objects[1]
-- And we break so we don't process any other object.
break
end
end
end
This is the simplest solution, although it reorders the objects a bit (swaps the top object with the one the click was registered with, regardless of its x and y position), a bit better solution would be to move the selected to the top, and slide all the objects that were above it by 1.