Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

If I have two existing display objects in Corona, lets say do1 and do2. Assume do1 is drawn first then do2, so do2 will be on top.

Is there a way to say "put do2 behind do1" in corona Lua? If yes can you give me an example?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

If do1 and do2 are in a display group they appear in the order you insert them as:

local group = display.newGroup()
group:insert(do1)
group:insert(do2)

Groups are also numerically indexed and the objects are in display order based on the index:

assert(group[1] == do1) -- do1 is on the bottom
assert(group[2] == do2) -- do2 is on the top (front)

If you happen to add your items out of order, you can move another item to the top in a number of ways:

local group = display.newGroup()
group:insert(do2) -- do2 is on the bottom
group:insert(do1) -- do1 is on the top (front)

-- Move do2 to front by re-inserting it - only one instance will exist in group
group:insert(do2)

assert(group[1] == do1) -- do1 is on the bottom
assert(group[2] == do2) -- do2 is on the top (front)

-- Move do2 to the front

do2:toFront()

assert(group[1] == do1) -- do1 is on the bottom
assert(group[2] == do2) -- do2 is on the top (front)

-- Move do2 to the back

do2:toBack()

assert(group[1] == do2) -- do2 is on the bottom
assert(group[2] == do1) -- do1 is on the top (front)

See also toFront() and toBack().

You an only put items in the front or back. As far as I know there's no way to arbitrarily place items. You can however maintain display order in another list and insert them based on that order.

share|improve this answer
add comment

If you don't need organization and don't need to change the display order later on, you can just do:

do2:toBack()

Or:

do1:toFront()

Now, if you want to maintain them later on, then put both do1 and do2 into a display group:

doGroup = display.newGroup()
doGroup:insert(do2)
doGroup:insert(do1)

The fact that do1 was inserted last means it will now appear at the top. Also, a display object such as do1 or do2 can only be in one display group at any time, so you have effectively taken them away from the default display "stage" (which all display objects are placed by default) and moved them to a new one (the doGroup), which is now in another order.

share|improve this answer
add comment

Just Googled this question and since it's still unanswered, here's the way to do it for others in need:

local group = display.newGroup()
group:insert(do1)

for n = 1, group.numChildren do
    if group[n] == do1 then
        group:insert(n, do2) -- this puts do2 behind do1
        break
    end
end
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.