Display group on front of buttons

In my app i have background image and as decorative i have objects falling from the top of the screen down.

The code for this is in the main.lua file at the end of that i use composer to go to the first scene.

In that scene the falling objects are behind the button. But to scenes further the are not. 

In between i have to stop the spawning and delete all the falling objects on the screen.

Main.lua:

bg = display.newImage( visuals.bgImage, w/2, h/2, true) --code for background objects local gameValues = require("gameValues") local mathRandom = math.random bgObjects = display.newGroup() local bgTimer function startbgObjects() bgTimer = timer.performWithDelay( gameValues.spawnTime, spawnbgObject, 0 ) end function stopbgObjects() timer.cancel( bgTimer ) display.remove( bgObjects ) bgObjects = display.newGroup() end --goto startscreen local composer = require("composer") composer.gotoScene( "StartScene", { effect="fade", time= 500 } )

The startObjects function is called in the scene.will show and

the stopObjects is called in the scene will hide.

I have tried to put the objects behind the button in the third scene by using:

bgObjects:toBack()

but this caused it to go behind the background image.

How can i make it that the objects go between the background and the button of the screen?

Thanks in regards.

Hi! Here are very experienced developers that will write a better answer, but what I know as a newbie is that main.lua is the first read module where you place some code to run in your app, like Android back button event key, but if you want to make a fallen objects effect in your scene I think it’s very important that you make your animation inside the scene module. I really do not understand the display.newImage syntax you use.

Hi @Cing2,

As dodi_games says, it’s almost always best (when using Composer) to use main.lua as a place to set up and initialize your app, not really use it as a “scene”. While you can do it otherwise, it just opens up other potential issues and I think you’d be better off approaching the entire app in a scene-based manner.

Brent

Well I have the code in the main.lua file because the background Objects are for multiple scene.

And because the Display.group where all the objects go in the is created before the scene it will go behind the scene.

After i looked at my code again i realized that this:

function stopbgObjects() timer.cancel( bgTimer ) display.remove( bgObjects ) bgObjects = display.newGroup() end

Remakes the display group so it isn’t in the background anymore.

So i changed the code to this and now the objects stay behind the scenes:

function stopbgObjects() timer.cancel( bgTimer ) for a=1, bgObjects.numChildren,1 do if bgObjects[a] ~= nil then display.remove( bgObjects[a] ) end end end

Still thank you @dodi_games and @Brent Sorrentino for the advise  :slight_smile:

The best way to make sure things stay either “in front” or “behind” scenes in a Composer-based design is to use the composer “stage” and sort/layer things that way.

https://docs.coronalabs.com/api/library/composer/stage.html

Brent

Hi! Here are very experienced developers that will write a better answer, but what I know as a newbie is that main.lua is the first read module where you place some code to run in your app, like Android back button event key, but if you want to make a fallen objects effect in your scene I think it’s very important that you make your animation inside the scene module. I really do not understand the display.newImage syntax you use.

Hi @Cing2,

As dodi_games says, it’s almost always best (when using Composer) to use main.lua as a place to set up and initialize your app, not really use it as a “scene”. While you can do it otherwise, it just opens up other potential issues and I think you’d be better off approaching the entire app in a scene-based manner.

Brent

Well I have the code in the main.lua file because the background Objects are for multiple scene.

And because the Display.group where all the objects go in the is created before the scene it will go behind the scene.

After i looked at my code again i realized that this:

function stopbgObjects() timer.cancel( bgTimer ) display.remove( bgObjects ) bgObjects = display.newGroup() end

Remakes the display group so it isn’t in the background anymore.

So i changed the code to this and now the objects stay behind the scenes:

function stopbgObjects() timer.cancel( bgTimer ) for a=1, bgObjects.numChildren,1 do if bgObjects[a] ~= nil then display.remove( bgObjects[a] ) end end end

Still thank you @dodi_games and @Brent Sorrentino for the advise  :slight_smile:

The best way to make sure things stay either “in front” or “behind” scenes in a Composer-based design is to use the composer “stage” and sort/layer things that way.

https://docs.coronalabs.com/api/library/composer/stage.html

Brent