set default background for all in a require file

Hi y’all,
I would like to know how to get Director recognize an outside file to be used as a background.

I have the following for my background.lua code (note, there’s a lot more than just the background, but I’m testing just with one image and it still acts weird):

-- background  
local function backgroundImg()  
 local bg = display.newImage( "bg.png" )  
 bg.width = 480  
 bg.height = 320  
 bg.x = 240  
 bg.y = 160  
end  
backgroundImg()  

On my menu.lua file, I have the following:

function new()  
local localGroup = display.newGroup()  
  
-- load background  
local bgImg = require( "background" )  
  
return localGroup  
end  

At this point, my background loads fine…

however, when I add additional images to my menu.lua file:

function new()  
local localGroup = display.newGroup()  
  
-- load background  
local bgImg = require( "background" )  
-- play button  
local play\_btn = display.newImage("playbtn.png", 203, 165)   
localGroup:insert(play\_btn)  
  
return localGroup  
end  

If I go run the above, I get the ‘play button’ image, but now I lost my background.

If I go back to my background.lua file and add the following:

function new()  
local localGroup = display.newGroup()  
  
-- background  
local function backgroundImg()  
 local bg = display.newImage( "bg.png" )  
 bg.width = 480  
 bg.height = 320  
 bg.x = 240  
 bg.y = 160  
 localGroup:insert(bg)  
end  
backgroundImg()  
return localGroup  
end  

If I run again from my menu.lua file…now the background shows, but NOT the ‘play button’ :frowning:

What am I doing wrong?

Any help will be greatly appreciated.
Thanks,
RD [import]uid: 7856 topic_id: 6701 reply_id: 306701[/import]

Anybody?
I mainly want to know if I’m doing something wrong and/or if there’s another way to require outside files with animation and physics on it with director…if so, how?

Thanks,
RD [import]uid: 7856 topic_id: 6701 reply_id: 23808[/import]

I’ve been trying to basically do the same!! I can get either one to work but annoyingly I can create a level with required controls.lua but then that breaks the button but because I have music playing I can hear the menu music playing so it is trying to switch but just can’t get rid of the controls.lua file.

I’m starting to think this is just a limitation or something because I’ve not come across a solution as of yet… [import]uid: 33866 topic_id: 6701 reply_id: 24011[/import]

You didn’t write it, but I’m assuming your files all have module(…, package.seeall) right? (except your main.lua) Otherwise you’ll be getting errors.

I copied your code and placed sample assets “bg.png” and “playbtn.png” and cannot reproduce your problem, other than the display order placing the background on top of the button. One issue you have is that it looks like you want to control the background class, but right now you have no control over it. You aren’t returning the image so you can’t insert it into the localGroup in menu.lua, and neither is anything else exposed. Also, when you wrap backgroundImg() with the new() method, you aren’t calling new() anywhere so your image is never being created.

The reason your modules need new() to work with the director class is because the director class calls new() in whatever file you send it to.

@cl-apps Make sure you are inserting whatever display object you’re using from controls.lua into it’s parent file - the director class only knows about object in localGroup! If you don’t want them in localGroup you’ll need to do your own cleanup.

Hopefully this helps! [import]uid: 12405 topic_id: 6701 reply_id: 24515[/import]