Help With External Modules & Inserting Into Groups

Hi everyone,

I’m transitioning my game from a one file mess to using external modules and Director but I’m having a problem with inserting images to groups. I’m guessing it’s easy to do and I tried searching for answers but I just can’t figure it out.

Basically I have main.lua to launch game.lua scene and inside game.lua I have it localizing game_init.

I get an error that says "attempt to index global ‘groupBg’ (a nil value) when I try to insert “bg” into “groupBg” (line 9 of game_init.lua). Any help would be great!
Edit: I forgot to mention that there is other images that I want to add to other groups within “game_init.lua”. I read that other have used “return” to take the variable back but I don’t think would work in my case(?)

- game.lua  
  
module(..., package.seeall)  
  
new = function ()   
 ------------------  
 -- Groups  
 ------------------   
 local localGroup = display.newGroup()  
 groupBg = display.newGroup()  
 localGroup:insert( groupBg )  
  
 ------------------  
 -- Your code here  
 ------------------   
 local gameInit = require( "game\_init" )  
 gameInit.gameSetup()  
  
 ------------------  
 -- MUST return a display.newGroup()  
 ------------------   
 return localGroup   
end  

[code]

  • game_init.lua

module(…, package.seeall)

function gameSetup()
local bg = display.newImage( “background.png”, true )
bg.x = display.contentWidth / 2
bg.y = display.contentHeight / 2
groupBg:insert( bg )
end
[/code] [import]uid: 59128 topic_id: 16977 reply_id: 316977[/import]

Ah! Found the answer. FYI for those having the same problem:

http://developer.anscamobile.com/forum/2011/10/07/another-module-question [import]uid: 59128 topic_id: 16977 reply_id: 63694[/import]

Glad to hear you got it solved - and thanks so much for sharing the solution, it really helps when users do that :slight_smile:

Peach [import]uid: 52491 topic_id: 16977 reply_id: 63718[/import]

Very appreciate your feed back of answer. This is very useful for people who face same problem like me.

KC [import]uid: 94613 topic_id: 16977 reply_id: 65349[/import]