Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to set an background image across all scenes?
Started by ammar71 Apr 29 2019 02:33 AM

3 replies to this topic
display order display background

Best Answer SGS , 29 April 2019 - 03:33 AM

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

 

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 32 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 762 posts
  • Corona SDK

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.



[TOPIC: post.html]
#3

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,105 posts
  • Corona SDK

  Best Answer

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

 

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



[TOPIC: post.html]
#4

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 32 posts
  • Corona SDK

@XeduR @Spyric and @SGS

Thanks to both. Both links were useful. 




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: display order, display, background