swap depth order

Hi

Here is my first simple test in lua/corona. My problem is that the blue square is always behind the yellow square. How can I bring the blue square to the foreground when I drag it?

[lua]local square1 = display.newRect( 20, 20, 100, 100 )
square1:setFillColor(0, 200, 255)

local square2 = display.newRect( 220, 380, 100, 100 )
square2:setFillColor(230, 230, 0)
function dragSquare1 (event)
if event.phase == “began” then
square2:removeEventListener(“touch”, dragSquare2)
end

if event.phase == “moved” then
square1.y = event.y
square1.x = event.x
end
if event.phase == “ended” then
square2:addEventListener(“touch”, dragSquare2)
end
end

function dragSquare2 (event)
if event.phase == “began” then
square1:removeEventListener(“touch”, dragSquare1)
end

if event.phase == “moved” then
square2.y = event.y
square2.x = event.x
end
if event.phase == “ended” then
square1:addEventListener(“touch”, dragSquare1)
end
end
square1:addEventListener(“touch”, dragSquare1)
square2:addEventListener(“touch”, dragSquare2)[/lua] [import]uid: 10820 topic_id: 3245 reply_id: 303245[/import]

Objects are stacked in the order they are loaded
Look into putting your objects in groups then you can reorder them after they are loaded [import]uid: 7911 topic_id: 3245 reply_id: 9622[/import]

When you insert an object into a group you can define the position that the object is inserted into the group.

If you dont it goes in last, which means the object is the top of the stack.

You can reinsert an object into the same group and if you provide the position you want it then it will be reinserted at that level.

In this way you can form layers of groups, a background layer, a moving characters layer etc… and then within those groups position and layer everything an necessary.

I always make a master world group, that all other groups are inserted into in the correct order. The different components of the game, lua required files etc… when loaded can then look for the group they need from the global master group and then start populating that group. [import]uid: 5354 topic_id: 3245 reply_id: 9660[/import]

sounds very good. thank you :slight_smile: [import]uid: 10820 topic_id: 3245 reply_id: 9664[/import]

If you insert an object into the same group, it moves to the top of the group (and top of the display stack).

-Tom [import]uid: 7559 topic_id: 3245 reply_id: 9696[/import]

Tom

If you use group:insert( [index,] child, [, resetTransform] ) like in the documents you can control the position of the object in the group and hence the display stack

http://developer.anscamobile.com/reference/index/groupinsert

index
number: Inserts child at index into group, shifting up other elements as necessary. The default value index is n+1 where n is the number of children in the group. An easy way to move an object above its siblings is to re-insert it: object.parent:insert( object ).

child
object: An object to insert into the group.

resetTransform
boolean: Determines what happens to child’s transform. When false, child’s local position, rotation, and scale properties are preserved, except the local origin is now relative to the new parent group, not its former parent; when true, child’s transform is reset (i.e. the x, y, rotation, xScale, and yScale properties of child are reset to 0, 0, 0, 1, and 1, respectively). The default value for resetTransform is false. [import]uid: 5354 topic_id: 3245 reply_id: 9698[/import]

Matthew, I wasn’t disagreeing with you, just pointing out an easy way to bring an object to the top of the display stack. I guess I was too fast in my reply.

Thanks,
-Tom [import]uid: 7559 topic_id: 3245 reply_id: 9705[/import]