[SOLVED] Help with layering objects

Hello
 
Recently I’ve just started to use modules. The problem I’m facing while I’m using modules is calling an object from the module I created and putting it behind other objects instead of being in the front. Here is the example:
 
 

this is the object I created from a seperate folder.

box.lua

local t = {} local rectsit = display.newRect(0,0,50,50) rectsit.x = 240 rectsit.y = 140 local function doneit() rectsit:setFillColor(0,0,0) end rectsit:addEventListener("touch", doneit) return t

this is the scene I’m calling my box.lua file

menu.lua

module(..., package.seeall) function new() local localGroup = display.newGroup() local box = require("box") local tria = display.newRect(0,0,320, 500) tria.x = 240 tria.y = 160 localGroup:insert(tria) return localGroup end  

Now the problem is the object that I called is in front of the object “tria” how would I put the object I called behind “tria”.

Thanks

localGroup is created before box. This makes localGroup “underneath” box in the display layering.

Then, tria is created. At this point, if nothing else happened to the two, tria is on top of box in the display system.

Next, tria is inserted into localGroup. This places tria at localGroup depth priority (behind box).


Alternately, you can add rectSit:toBack() to your box.lua…

It sounded like everything you explain is what I already have right now or maybe I did not understand. Can you please explain it to me in code?

How about this tidbit then…

You said you were new at modules. One thing about modules in lua…

All code that is OUTSIDE of function in a module is executed when it’s require is processed. For your example, this means that when menu requires box, all the code in box that is not inside a function executes (including the newRect call)

And with corona, objects created first are layered behind objects created later.

So, if menu first creates localGroup, then “requires” box (which creates the rectsit)… localGroup will be behind box (and everything inside localGroup). Once tria is inserted into localGroup, it takes the priority of localGroup of course (underneath rectsit).

Ok I got it now I was about to ask you to it break down further but I end up getting it. I did this which sounded like the same way you said it in english

box.lua

local t = {} local rectsit = display.newRect(0,0,50,50) rectsit.x = 240 rectsit.y = 140 local function doneit() rectsit:setFillColor(0,0,0) end rectsit:addEventListener("touch", doneit) return t  

menu.lua

module(..., package.seeall) function new() local localGroup = display.newGroup() local tria = display.newRect(0,0,320, 500) tria.x = 240 tria.y = 160 local function layers () if localGroup then local box = require("box") else localGroup:insert(tria) end end layers() return localGroup end  

Well, that looks like it put rectsit in front of tria, but…

The way your code executes now, tria is never put into the group I think. When menu:new executes, it now does this:

  • creates localgroup

  • creates tria

  • calls layers() – which then does the require"box"

  • returns the pointer to localgroup - which tria was never inserted into (but its below rectsit, since rectsit was created last)

I think your difficulty is related to your rectsit being created OUTSIDE of a function call… And your use of the require way down in processing code hints you might be thinking of modules and requires as functions… Maybe a little code would be more illustrative… (didn’t get to build/test it, but hopefully it’ll convey the idea).

[lua]–Box.lua


local rectsit

local function doneit()

    rectsit:setFillColor(0,0,0)
end

function new() — this code, now in a function, only executes when box:new() is called…

    rectsit = display.newRect(0,0,50,50)

    rectsit.x = 240

    rectsit.y = 140

    rectsit:addEventListener(“touch”, doneit)

return rectsit

end

[/lua]

[lua]-- menu.lua

module(…, package.seeall)

local boxModule = require(“box”)      — no box is created yet… All code is inside functions there…

function new()
local localGroup = display.newGroup()

local tria = display.newRect(0,0,320, 500)
tria.x = 240
tria.y = 160
 

localGroup:insert(tria)

local box = boxModule:new()   – returns a pointer to a box object

localGroup:insert(box)   – insert the box object into the group - now box is in front

box:toBack()   – Now box is in back of tria

return localGroup

end[/lua]

Ok make sense now i had an error at first, but I fixed it now the error was that

module(...,package.seeall)  

was not written in the box.lua that was the last missing code. Perfect, now I’m starting to get understand how these modules work thanks for the help.

Glad if I helped…

Looking at how my code is formatted above… not sure if I did, hehe

localGroup is created before box. This makes localGroup “underneath” box in the display layering.

Then, tria is created. At this point, if nothing else happened to the two, tria is on top of box in the display system.

Next, tria is inserted into localGroup. This places tria at localGroup depth priority (behind box).


Alternately, you can add rectSit:toBack() to your box.lua…

It sounded like everything you explain is what I already have right now or maybe I did not understand. Can you please explain it to me in code?

How about this tidbit then…

You said you were new at modules. One thing about modules in lua…

All code that is OUTSIDE of function in a module is executed when it’s require is processed. For your example, this means that when menu requires box, all the code in box that is not inside a function executes (including the newRect call)

And with corona, objects created first are layered behind objects created later.

So, if menu first creates localGroup, then “requires” box (which creates the rectsit)… localGroup will be behind box (and everything inside localGroup). Once tria is inserted into localGroup, it takes the priority of localGroup of course (underneath rectsit).

Ok I got it now I was about to ask you to it break down further but I end up getting it. I did this which sounded like the same way you said it in english

box.lua

local t = {} local rectsit = display.newRect(0,0,50,50) rectsit.x = 240 rectsit.y = 140 local function doneit() rectsit:setFillColor(0,0,0) end rectsit:addEventListener("touch", doneit) return t  

menu.lua

module(..., package.seeall) function new() local localGroup = display.newGroup() local tria = display.newRect(0,0,320, 500) tria.x = 240 tria.y = 160 local function layers () if localGroup then local box = require("box") else localGroup:insert(tria) end end layers() return localGroup end  

Well, that looks like it put rectsit in front of tria, but…

The way your code executes now, tria is never put into the group I think. When menu:new executes, it now does this:

  • creates localgroup

  • creates tria

  • calls layers() – which then does the require"box"

  • returns the pointer to localgroup - which tria was never inserted into (but its below rectsit, since rectsit was created last)

I think your difficulty is related to your rectsit being created OUTSIDE of a function call… And your use of the require way down in processing code hints you might be thinking of modules and requires as functions… Maybe a little code would be more illustrative… (didn’t get to build/test it, but hopefully it’ll convey the idea).

[lua]–Box.lua


local rectsit

local function doneit()

    rectsit:setFillColor(0,0,0)
end

function new() — this code, now in a function, only executes when box:new() is called…

    rectsit = display.newRect(0,0,50,50)

    rectsit.x = 240

    rectsit.y = 140

    rectsit:addEventListener(“touch”, doneit)

return rectsit

end

[/lua]

[lua]-- menu.lua

module(…, package.seeall)

local boxModule = require(“box”)      — no box is created yet… All code is inside functions there…

function new()
local localGroup = display.newGroup()

local tria = display.newRect(0,0,320, 500)
tria.x = 240
tria.y = 160
 

localGroup:insert(tria)

local box = boxModule:new()   – returns a pointer to a box object

localGroup:insert(box)   – insert the box object into the group - now box is in front

box:toBack()   – Now box is in back of tria

return localGroup

end[/lua]

Ok make sense now i had an error at first, but I fixed it now the error was that

module(...,package.seeall)  

was not written in the box.lua that was the last missing code. Perfect, now I’m starting to get understand how these modules work thanks for the help.

Glad if I helped…

Looking at how my code is formatted above… not sure if I did, hehe