How to set an background image across all scenes?

Hi all,

I have an image that I want to be the background of all my scenes. I tried loading the image in main.lua

background\_all = display.newImageRect( "graphics/clouds.png", 768, 1024 ) background\_all.x = display.contentCenterX background\_all.y = display.contentCenterY composer.gotoScene( "intro\_screen")

Then in intro_screen.lua I have:

function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen local background = display.newImageRect( sceneGroup, "graphics/intro\_screen.png", 768, 1024 ) background.x = display.contentCenterX background.y = display.contentCenterY end

Where the intro_screen.png is transparent.

The intro_screen.png doesn’t show. I think the cloude.png is on top of it.

If I add buttons (widget.newButton) then those show on top of the clouds.png but if I add them to the scene, they disappear.

I tried to add background:toFront() in intro_screen.lua but still I can’t see the intro_screen.png

I know that I can load the clouds.png in each scene instead of just once at the beginning but wouldn’t that use more memory?

What is the correct way to do this?

Thanks.

I would recommend creating local display objects in every scene. It is a few lines of extra code per scene, but that’s about it for the cons.

The benefits of doing so is that you’ll have easier time handling the objects/variables, you are less likely to create memory leaks, and most importantly, you’ll have easier time managing order of the display groups and objects on the screen. While you are worried about consuming more memory, the fact is that local variables run significantly faster than their global counterparts. If you are working with a limited number of global variables or you have non-time critical global variables, then it doesn’t really matter which you use performance wise, but locals are generally preferred in most cases.

You could/should take a peek here: https://docs.coronalabs.com/tutorial/basics/globals/index.html

From the looks of it, your “background_all” is a global display object without a group, which means that it will always be in front of other display objects that have been inserted into groups. Using “background:toFront()” won’t make a difference since “background_all” is not in a group, so it will always stay on top of other display objects that are in groups.

This is easy to achieve by inserting the image behind the composer stage.

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

@XeduR @Spyric and @SGS

Thanks to both. Both links were useful. 

I would recommend creating local display objects in every scene. It is a few lines of extra code per scene, but that’s about it for the cons.

The benefits of doing so is that you’ll have easier time handling the objects/variables, you are less likely to create memory leaks, and most importantly, you’ll have easier time managing order of the display groups and objects on the screen. While you are worried about consuming more memory, the fact is that local variables run significantly faster than their global counterparts. If you are working with a limited number of global variables or you have non-time critical global variables, then it doesn’t really matter which you use performance wise, but locals are generally preferred in most cases.

You could/should take a peek here: https://docs.coronalabs.com/tutorial/basics/globals/index.html

From the looks of it, your “background_all” is a global display object without a group, which means that it will always be in front of other display objects that have been inserted into groups. Using “background:toFront()” won’t make a difference since “background_all” is not in a group, so it will always stay on top of other display objects that are in groups.

This is easy to achieve by inserting the image behind the composer stage.

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

@XeduR @Spyric and @SGS

Thanks to both. Both links were useful.