hello everyone,
I have object1 and object2 and many other objects in a group in the scene. I want the object1 to be in front of object2. I used object1:toFront(), but that didn’t work. This must be done after all the objects are created, not by inserting object1 after insert object2 in that group when you create them.
( 2 objects do not collide with each other , so one can be in front of the other one). Any suggestion? [import]uid: 33695 topic_id: 16898 reply_id: 316898[/import]
use multiple groups [import]uid: 7911 topic_id: 16898 reply_id: 63350[/import]
jstrahan, can you give me a little bit more detail?
[import]uid: 33695 topic_id: 16898 reply_id: 63366[/import]
im not understanding why you can’t reinsert into group to move to top of list
thats all toFront basically does is moves it to the top of display list [import]uid: 7911 topic_id: 16898 reply_id: 63421[/import]
one reason is because you may want an animating object to remain on top (you just want that other object to be in front of another while the animating object in front remains undisturbed) [import]uid: 11382 topic_id: 16898 reply_id: 80572[/import]
use the index arg. when reinserting into the group to reinsert behind animation but on top of others [import]uid: 7911 topic_id: 16898 reply_id: 80602[/import]
You could use something like this to re-insert your object under the top object after you’ve created the group.
[lua]group:remove(object);
group:insert(group.numChildren, object)[/lua] [import]uid: 70847 topic_id: 16898 reply_id: 80604[/import]
but if you create 3 groups
mainGroup
aniGroup
imgGroup
then insert images into imgGroup
insert animations into aniGroup
then insert imgGroup into mainGroup
then insert aniGroup into mainGroup
this way your animation will always be on top of your images
and you can just use toFront or the index on the images to change its order
this is an example of using multiple groups [import]uid: 7911 topic_id: 16898 reply_id: 80608[/import]
no need to remove first. when you reinsert it removes it from original position
even when you reinsert into a different group it automatically removes from the original group
so when reinserting an image into a group think of it as a move not an insert [import]uid: 7911 topic_id: 16898 reply_id: 80605[/import]
I agree that using multiple groups is a solution! There will be instances though when using multiple groups just won’t do. Also, there’s a similar thread somewhere in the forums, but the solution that was presented involved transferring the elements to a temporary table before sorting based on a custom ‘layer’ field.
Here’s how I solved a similar problem on my end, code snippet taken from a working module. Error handling still needs to be improved:
-- Custom function attempting to move object in front of another
-- Object2 is the object to be placed in front of object1
-- mel@gobeyondstudios.com
function moveObjectOnTopOfObject(group, object1, object2)
local n = group.numChildren
local bottomI = 0
local topI = 0
if object1 == object2 then
print("error: object1 and object2 are the same!")
return false
end
for i = 1, n do
if group[i] == object1 then
bottomI = i
elseif group[i] == object2 then
topI = i
end
if topI ~= 0 and bottomI ~= 0 then
break
end
end
if bottomI == 0 or topI == 0 then
print("error: elements not found!")
return false
else
group:insert(bottomI+1, object2)
return true
end
end
Should there be a more efficient way to do this, I’d be happy to hear about it. [import]uid: 11382 topic_id: 16898 reply_id: 80617[/import]
any examples where multiple groups won’t work?
to simplify your code some you could set topI and bottomI to nil then remove the if statement to check if both =0 and use assert statement in its place
[import]uid: 7911 topic_id: 16898 reply_id: 80622[/import]