z-depth of new images

Hi,

I’m working on an app where I have to overlay an image over a layer of images that are cross-fading in a loop, but every time I load the next image in the loop it gets drawn over what is supposed to be the top level image.

I’m having trouble figuring out from the documentation if there is a way to manually assign the z-depth of a newImage(), any hints?

thanks [import]uid: 6693 topic_id: 1358 reply_id: 301358[/import]

You can’t do this on “display.newImage()” itself. It will always load your image to the top of the stage!

You can “insert()” it to another position by giving an index to the insert parameter.

group:insert( [index,] child, [, resetTransform] )   

I would suggest something different…

local layers=display.newGroup()  
  
local layer\_top=display.newGroup()  
local layer\_back=display.newGroup()  
  
layers:insert(layer\_back)  
layers:insert(layer\_top) -- gets on top as last inserted  
  
-- setup done  
  
layers.x=... -- place it where it should go  
layers.y=...  
  
-- loading first pic  
  
layer\_top:insert(display.newImage(...))  
  
-- loading second pic (still img1 on top)  
  
layer\_back:insert(display.newImage(...))  
  
-- loop starts here  
  
-- fade out img1 ...  
  
transition.to(layer\_top[1], ....)  
  
-- remove img1 .. move img2 to top  
  
layer\_top:remove(1)  
layer\_top:insert(layer\_back[1])  
  
-- load a new image into back layer  
  
layer\_back:insert(display.newImage(...))  
  
--- loop restart  
  

You need to have some controller for the loop of course.

This way it is always clear which layer contains what and you do not need to fiddle with indexes in the stage object. [import]uid: 6928 topic_id: 1358 reply_id: 3717[/import]

Awesome, thanks Oderwat!

Works like a charm, and has given me a much better understanding of how groups work.

I have to admit it’s still a tad confusing that inserting a new object into the bottom layer without removing anything doesn’t grow the number of objects in the group. My understanding from the API ref is that using insert will just add the object to the end of the group, not replace the first object.

Anywho, works and moving on.

cheers [import]uid: 6693 topic_id: 1358 reply_id: 3721[/import]

Glad it helped (and worked)! [import]uid: 6928 topic_id: 1358 reply_id: 3722[/import]

ahh ok, so it seems that inserting and object from one group into another removes it from the first

that clears a few things up, I thought it just made a copy, like setting one variable equal to another

good to know [import]uid: 6693 topic_id: 1358 reply_id: 3737[/import]