I have a Corona project that has about a half dozen serparate lua files to help keep things organized. I’m now trying to integrate the director class for transitions between a menu screen and the game itself, but get errors when trying to return to a ‘scene’ that’s already been viewed IF that scene calls any external lua files to add objects.
While the real files are significantly more convoluted, as a test I’ve set up a bare-bones scenario of main.lua, scene1.lua, scene2.lua and otherstuff.lua.
otherstuff.lua contains 3 simple lines to make a text object called othertext:
[lua]othertext = display.newText( “Here’s some other text from a different lua file”, 0, 0, native.systemFontBold, 16 )
othertext.x = 400
othertext.y = 700[/lua]
main.lua is straight out of the director sample app, requires director, creates a new group, then loads in scene 1.
scene1.lua is again pretty much straight of out the sample app - displays a line of text, makes a button that goes to “scene2”, then sticks both into localGroup.
scene2.lua is where I’m having a hard time. Like scene1, I’m just trying to display a line of text, make a button then add them both to a group. Unlike scene1 however, I’m trying to include the line of text using require( “otherstuff” ) instead of creating it directly in the scene function; like so (line 5):
[lua]module(…, package.seeall)
new = function ( params )
local otherstuff = require( “otherstuff” ) – Here’s where I’m requiring the file that contains the object I want to put in the group.
local localGroup = display.newGroup()
local titletext2 = display.newText( “this is scene 2”, 250, 202, native.systemFontBold, 18 )
local abutton2 = display.newRect( 200, 300, 300, 300, 6 )
local initVars = function ()
localGroup:insert( titletext2 )
localGroup:insert( abutton2 )
localGroup:insert( othertext )
abutton2:addEventListener( “tap”, abutton2 )
function abutton2:tap( event )
print(“going to scene 1”)
director:changeScene( “scene1”, “moveFromRight” )
end
end
initVars()
return localGroup
end[/lua]
When I run the test app it starts me off on scene1 as expected, I click on the button and transition to scene2 ok, then click on scene 2’s button to go back to scene1 with no problems. When I click on the button to go back to scene2 at that point, I get the errors:
From a little pop-up: Director ERROR: Falied to execute new( params ) function on ‘scene2’.
And in the Simulator Output:
going to scene 2
going to scene 1
going to scene 2
Runtime error
...rs\matt\documents\corona projects\test2\director.lua:1092: attempt t
call method 'insert' (a nil value)
stack traceback:
[C]: in function 'insert'
...rs\matt\documents\corona projects\test2\director.lua:1092: in functi
n 'changeScene'
-----------------------
Director ERROR: Failed to execute new( params ) function on 'scene2'.
-----------------------
...sers\matt\documents\corona projects\test2\scene2.lua:14: bad argument #-2 to
'insert' (Proxy expected, got nil)
-----------------------
At the bottom there it’s mentioning line 14 in scene 2, which is:
[lua]localGroup:insert( othertext )[/lua]
…so while I’m not sure why, I’m pretty sure that ‘othertext’ is not available the second time around to be inserted into the group?
If I take out:
[lua]local otherstuff = require( “otherstuff” )[/lua]
and put the text object directly where that line previously was (in scene2) instead of getting it through otherstuff.lua, i.e:
[lua]module(…, package.seeall)
new = function ( params )
othertext = display.newText( “Here’s some other text from a different lua file”, 0, 0, native.systemFontBold, 16 )
othertext.x = 400
othertext.y = 700
local localGroup = display.newGroup()
print (table.concat(localGroup))
local titletext2 = display.newText( “this is scene 2”, 250, 202, native.systemFontBold, 18 )
local abutton2 = display.newRect( 200, 300, 300, 300, 6 )
local initVars = function ()
localGroup:insert( titletext2 )
localGroup:insert( abutton2 )
localGroup:insert( othertext )
abutton2:addEventListener( “tap”, abutton2 )
function abutton2:tap( event )
print(“going to scene 1”)
director:changeScene( “scene1”, “moveFromRight” )
end
end
initVars()
return localGroup
end[/lua]
Then everything works fine; there’s no error the second time around on scene2.
Am I not permitted to use require in the function like that, coding it wrong or newbie’ing it up some other way? I can ‘fix’ it by including each lua files code directly in one now massive ‘scene2’ file, but I’d rather not (it’s a few thousand lines altogether if I do).
Any tips/suggestions/points in the right direction much appreciated! [import]uid: 49694 topic_id: 21195 reply_id: 321195[/import]