Display objects don't appear in the order they are inserted into a scene group (Code Attached)

Hello! Here’s the gist of my issue: I have three display groups - self.Backdrop, self.Tile, self.Creatures - respectively they are inserted into another display group called self.

According to readings on Corona API and posts on display groups, the order in which the display objects appear follows the order in which the display groups are inserted into the scene group. However, that doesn’t seem to be the case in my program.

As mentioned, I have inserted self.Backdrop first, self.Tile second, then self.Creatures third. So, the images should stack with self.Backdrop being in the background, self.Tile on the top of self.Backdrop , then self.Creatures on top of all the images. But, that’s not what I’m getting. self.Creatures appear in between self.Backdrop and self.Tile. I’d like to know why so I can fix this issue.

I also confirmed whether the objects were inserted in the correct order by outputting the names of all the table elements in the self group table.

The table numbers for each display group are the following:

self.Backdrop: 0DE56658

self.Scroll:       0DE56710

self.Creatures:0DE56B20

The elements of the self group table appear in the following order: 0DE56658, 0DE56710, 0DE56B20. So, essentially the display groups should appear in the order that corresponds the order in which display groups are inserted in to self group. But, I don’t know why that isn’t the case.

Here’s a snippet of the code:

return function(options) local self = display.newGroup() if options.Backdrop then self.Backdrop = display.newImage(self, options.Backdrop, w/2, 0) self.Backdrop.anchorY = 0 end local tilePath = options.Tile if tilePath then --self.Scroll = display.newGroup(); self:insert(self.Scroll) function self:StartBackground() local tile = display.newImage(tilePath, display.contentWidth, 0) tile.anchorX = 0 tile.anchorY = 1 tile.Shift = transition.to(tile, {time = 4000, delta = true, onComplete = function(...) return self:StartBackground() end; x = -2\*display.contentWidth, y = 2\*display.contentHeight}) end end self.Inhabitants = options.Inhabitants self.Scroll = display.newGroup(); self:insert(self.Scroll) self.Creatures = display.newGroup(); self:insert(self.Creatures) -- Event Listening Function function self:Game(event) function self:Game(event) if event.action == 'start' then start(self, event.duration) elseif event.action == 'stop' then stop(self) end end local function retransmit(...) self:dispatchEvent(...) end function self:remove(event) self:dispatchEvent{name = 'Despawn'; unit = event.target} end function self:Spawn(info) local newSpawn = self.Inhabitants[info.kind](self.Creatures, info.x, info.y, unpack(info)) newSpawn:addEventListener('Death', retransmit) newSpawn:addEventListener('remove', self) end return self end

Unless I’ve missed it somewhere, it appears to me that “tile” is not inserted into “self” (or into any group at all for that matter).

I can see that this is commented out:

--self.Scroll = display.newGroup(); self:insert(self.Scroll)

Then you create the tile, and do not insert it into anything. 

So you have a group called self, which contains self.Backdrop, self.Scroll and then self.Creatures. Then you have “tile” which is created after self, and so appears above it.

This is a rough interpretation of your current groups:

self = { Backdrop, Inhabitants, Scroll, Creatures } tile

And you need to insert tile into either the self group, or one of the other groups so that the order is like so:

self = { Backdrop, tile, Inhabitants, Scroll, Creatures } --or self = { Backdrop, Inhabitants, Scroll = { tile }, Creatures }

Yes, that was the fix. It was a silly mistake.

Just as an FYI:

The table numbers for each display group are the following:

self.Backdrop: 0DE56658

self.Scroll:       0DE56710

self.Creatures:0DE56B20

Those are memory addresses. The OS gives up memory as it frees up. It’s very likely that the memory addresses will not be in any form of sequential order and it cannot be used as a measure of object ordering.

Unless I’ve missed it somewhere, it appears to me that “tile” is not inserted into “self” (or into any group at all for that matter).

I can see that this is commented out:

--self.Scroll = display.newGroup(); self:insert(self.Scroll)

Then you create the tile, and do not insert it into anything. 

So you have a group called self, which contains self.Backdrop, self.Scroll and then self.Creatures. Then you have “tile” which is created after self, and so appears above it.

This is a rough interpretation of your current groups:

self = { Backdrop, Inhabitants, Scroll, Creatures } tile

And you need to insert tile into either the self group, or one of the other groups so that the order is like so:

self = { Backdrop, tile, Inhabitants, Scroll, Creatures } --or self = { Backdrop, Inhabitants, Scroll = { tile }, Creatures }

Yes, that was the fix. It was a silly mistake.

Just as an FYI:

The table numbers for each display group are the following:

self.Backdrop: 0DE56658

self.Scroll:       0DE56710

self.Creatures:0DE56B20

Those are memory addresses. The OS gives up memory as it frees up. It’s very likely that the memory addresses will not be in any form of sequential order and it cannot be used as a measure of object ordering.