Director + External Modules problem

Hi!

In my game I got three screens: mainmenu, levelmenu and myLevel, all switched with director. I also got a level.lua which myLevel requires from.

The problem is that when I start the game I can go to the mainmenu, then the levelmenu and then myLevel. But if I then go from myLevel to the mainmenu, then the levelmenu and then myLevel again I got this error:

bad argument #-2 to ‘insert’ (Proxy expected, got nil)

What to do?
Oskar
[import]uid: 24111 topic_id: 12465 reply_id: 312465[/import]

don’t have enough details to find what the problem really is…
will be easier if you can post the code here…

you may also check whether you have included all the below items in all the above mentioned files
[lua]module(…, package.seeall)
local localGroup = display.newGroup()
function new()
return localGroup
end[/lua]

also make sure that all your display objects inside your files are “declared as local” and that they are “inserted into a local group which is returned in the new() function”.

a some what similar problem was discussed here.
http://developer.anscamobile.com/forum/2011/07/13/backbutton-issues#comment-45535

[import]uid: 71210 topic_id: 12465 reply_id: 45540[/import]

I get the error when I try to insert the group that I have in the level.lua file, to the localGroup in the myLevel.lua file when I enter the level a second time. [import]uid: 24111 topic_id: 12465 reply_id: 45581[/import]

If you can post the relevant code I can fix it for you… [import]uid: 71210 topic_id: 12465 reply_id: 45582[/import]

main.lua:

print();print("");print()  
-- HIDE THE STATUS BAR  
display.setStatusBar( display.HiddenStatusBar )  
-- Import director class  
local director = require("director")  
  
-- The main group  
local mainGroup = display.newGroup()  
  
-- The main function  
local function main()  
 -- Add the group from director class  
 mainGroup:insert(director.directorView)  
  
 -- Change scene to mainmenu  
 director:changeScene("mainmenu", "fade")  
  
 return true  
end  
  
-- Start!"#!"#!"  
main()  
  

mainmenu.lua:

module(..., package.seeall)  
  
-- Main function - MUST return a display.newGroup()  
function new()  
 local localGroup = display.newGroup()  
  
  
 mainmenuText = display.newText("main menu", 50,50,system.defaultFont, 50)   
 mainmenuText.x = 500  
 mainmenuText.y = 500  
 localGroup:insert(mainmenuText)  
  
----FUNCTIONS  
  
 function levelmenu(event)  
 if event.phase == "ended" then  
 director:changeScene("levelmenu")  
 end  
 end  
  
----ADD EVENT LISTENERS  
 mainmenuText:addEventListener("touch", levelmenu)  
  
 -- MUST return a display.newGroup()  
 return localGroup  
end  
  

levelmenu.lua:

module(..., package.seeall)  
  
-- Main function - MUST return a display.newGroup()  
function new()  
 local localGroup = display.newGroup()  
  
  
 levelmenuText = display.newText("level menu", 50,50,system.defaultFont, 50)   
 levelmenuText.x = 500  
 levelmenuText.y = 500  
 localGroup:insert(levelmenuText)  
  
----FUNCTIONS  
  
 function myLevel()  
  
 director:changeScene("myLevel")  
 end  
  
----ADD EVENT LISTENERS  
 Runtime:addEventListener("touch", myLevel)  
 return localGroup  
end  

myLevel.lua:

[code]
module(…, package.seeall)
– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()
local level = require(“level”)
localGroup:insert(level.localGroup)

function startTest()
director:changeScene(“mainmenu”)
end

myLevelText = display.newText(“myLevel”, 50,50,system.defaultFont, 50)
myLevelText.x = 500
myLevelText.y = 500
localGroup:insert(myLevelText)

myLevelText:addEventListener(“touch”, startTest)

– MUST return a display.newGroup()
return localGroup
end

[/code] [import]uid: 24111 topic_id: 12465 reply_id: 45610[/import]

What is it inside your level.lua file ? [import]uid: 71210 topic_id: 12465 reply_id: 45622[/import]

level.lua:

[code]

module(…, package.seeall)
localGroup = display.newGroup()

[/code] [import]uid: 24111 topic_id: 12465 reply_id: 45625[/import]

try this…
create a function in level.lua file which return the group and call the function from your file.
see the code below

level.lua
[lua]module(…, package.seeall)
function new()
print(“in new”)
local localGroup = display.newGroup()
local txt = display.newText(“from level”,200,200,nil,25)
localGroup:insert(txt)
return localGroup
end[/lua]

to call it use
[lua]local level = require(“level”)
local levelgroup = level.new()
localGroup2:insert(levelgroup)[/lua]

hope this helps :slight_smile: [import]uid: 71210 topic_id: 12465 reply_id: 45631[/import]

I get this error when I try it:
Runtime error
/Users/Username/Desktop/myLevel.lua:6: attempt to index global ‘localGroup’ (a nil value)
[import]uid: 24111 topic_id: 12465 reply_id: 45648[/import]

I almost, almost, almost got it fixed now.

Just one little problem:

The old problem is gone (which is great! :smiley: ) but now when i enter the myLevel a second time and have a function at the end which sets some layers toFront() i get a new error:
attempt to call method ‘toFront’ (a nil value)

Note: This only when I already have entered myLevel, gone to the mainmenu, levelmenu and then tries to get back to the myLevel again. It works if i delete the toFront() function. [import]uid: 24111 topic_id: 12465 reply_id: 45697[/import]

Can you post the code please ? [import]uid: 71210 topic_id: 12465 reply_id: 45709[/import]

Fixed it by making the function at the end a local function instead of a global function.

Thanks for all your help with the other things and sorry for being late with posting code. [import]uid: 24111 topic_id: 12465 reply_id: 45752[/import]