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]