Display order of images on the screen

When am displaying each of the images one after the other in display order, it isn’t working with a function of creating images. I have a background image, then the clouds passing behind as a function of random image table, then the hero. 

Obviously the clouds has to pass behind the Hero, but it is passing through front. What to do? Why it is happening? Is it because of function update timer?

Objects will be displayed in the order they were added to the scene group. First = back, Last = front.

You have two options: either ensure the hero is created last, or before your game starts call hero:toFront() to bring him in front of the clouds.

In addition to nick’s answer, you can and should use display groups to enforce layering.

MAY CONTAIN TYPOS

local underlay = display.newGroup() -- bottom of stack local clouds = display.newGroup() local content = display.newGroup() local guis = display.newGroup() -- top of stack local bg = display.newImageRect( underlay, "background.png", 960, 640 ) bg.x = display.contentCenterX bg.y = display.contentCenterY local player = = display.newImageRect( content, "player.png", 50, 50) player.x = display.contentCenterX player.y = display.contentCenterY -- clouds behind player even though created later local left = display.contentCenterX - dispay.actualContentWidth/2 local right = display.contentCenterX + dispay.actualContentWidth/2 local stepMin = 100 local stepMax = 300 local x = left + 100 while( x \< right ) do local y = math.random( -200, 200 ) local cloud = = display.newImageRect( clouds, "cloud.png", 100, 40 ) cloud.x = x cloud.y = y x = x + math.random( stepMin, stepMax ) end

Indeed, as RG says if you had separate display groups for background, clouds and characters, it wouldn’t matter in which order individual objects were created, as long as you inserted the groups into the scene group in the correct order.

AWESOMEST… Thank you all. It worked. I wasn’t doing with scene management. Even though am very clear now the importance of groups. Wonderful.

Objects will be displayed in the order they were added to the scene group. First = back, Last = front.

You have two options: either ensure the hero is created last, or before your game starts call hero:toFront() to bring him in front of the clouds.

In addition to nick’s answer, you can and should use display groups to enforce layering.

MAY CONTAIN TYPOS

local underlay = display.newGroup() -- bottom of stack local clouds = display.newGroup() local content = display.newGroup() local guis = display.newGroup() -- top of stack local bg = display.newImageRect( underlay, "background.png", 960, 640 ) bg.x = display.contentCenterX bg.y = display.contentCenterY local player = = display.newImageRect( content, "player.png", 50, 50) player.x = display.contentCenterX player.y = display.contentCenterY -- clouds behind player even though created later local left = display.contentCenterX - dispay.actualContentWidth/2 local right = display.contentCenterX + dispay.actualContentWidth/2 local stepMin = 100 local stepMax = 300 local x = left + 100 while( x \< right ) do local y = math.random( -200, 200 ) local cloud = = display.newImageRect( clouds, "cloud.png", 100, 40 ) cloud.x = x cloud.y = y x = x + math.random( stepMin, stepMax ) end

Indeed, as RG says if you had separate display groups for background, clouds and characters, it wouldn’t matter in which order individual objects were created, as long as you inserted the groups into the scene group in the correct order.

AWESOMEST… Thank you all. It worked. I wasn’t doing with scene management. Even though am very clear now the importance of groups. Wonderful.