Getting into a bit of a muddle with structuring my code and scoping
I have a scene game.lua
In this scene I create 3 main display groups as follows:
local keyFramesClass = require("keyFrames")
M.new = function ()
local localGroup = display.newGroup()
local backgroundGroup = display.newGroup()
local foregroundGroup = display.newGroup()
local interfaceGroup = display.newGroup()
localGroup:insert(backgroundGroup)
localGroup:insert(foregroundGroup)
localGroup:insert(interfaceGroup)
displayKeyFrame("startGame")
I want to add a graphic to one of these display groups from another “module” called keyFrames.lua
local difficultyLevelPanelClass= require("difficultyLevelPanel")
function displayKeyFrame(passedKeyFrameID)
if (passedKeyFrameID == "startGame") then
local difficultyLevelPanel = difficultyLevelPanelClass.newDifficultyLevelPanel()
foregroundGroup:insert(difficultyLevelPanel)
I am getting the following error
attempt to index global ‘foregroundGroup’ (a nil value)
Any ideas what I am doing wrong here?
[import]uid: 7863 topic_id: 16067 reply_id: 316067[/import]
I assume that if I had made the 3 display groups global by removing the local prefix that I could access them from all my modules? Would this be a big no no?
Passing the path to the foreground in could be problematic as I call the displayKeyFrame function from other modules such as one called myGameUtils.lua
main.lua
require("myGameUtils")
myGameUtils.lua
[code]
function nextKeyframe()
local nextKeyframe = gameState:getState(“audioCompleteDestination”)
displayKeyFrame(nextKeyframe)
end
function NarrationFinished(event)
if event.completed then
nextKeyframe()
else
print(“Narration was stopped before completion”)
end
end
function stopAllSounds()
audio.stop()
end
function playAudio(audioId, destination, playOrStop)