Hello
I’ve got 3 files - 2 for levels and one - main.lua.
Here is the code:
-- level1.lua
module(..., package.seeall)
function new()
local newGroup = display.newGroup();
local btn1 = display.newImage( "btn\_play.png" )
newGroup:insert( btn1 )
newGroup.physObjects = {}
newGroup.physObjects[1] = btn1
newGroup.hole = 6
newGroup.isvisible = false
return newGroup
end
function startLevel( level )
level.isvisible = true
print( table.maxn( level.physObjects ) )
for i,v in pairs(level.physObjects) do
physics.addBody( v, { friction=0.3, bounce=0.7 } )
end
end
function endLevel( level )
level.isvisible = false
level:removeSelf()
end
-- level2.lua
module(..., package.seeall)
function new()
local levelObj = display.newGroup();
local btn1 = display.newImage( "btn\_rules.png" )
levelObj:insert( btn1 )
levelObj.physObjects = {}
levelObj.physObjects[1] = btn1
levelObj.hole = 6
levelObj.isVisible = false
return levelObj
end
function startLeve( level )
level.isVisible = true
for i,v in pairs(level.physObjects) do
physics.addBody( v, { friction=0.3, bounce=0.7 } )
end
end
function endLevel( level )
level.isVisible = false
level:removeSelf()
end
-- main.lua
local physics = require("physics")
physics.start()
physics.setGravity( 0, 9.8 )
local levels = { require("level1"), require("level2") }
local level1 = levels[1]
level1Obj = level1:new()
print( table.maxn( level1Obj.physObjects ) )
level1:startLevel( level1Obj )
The problem occures in level1.lua when pairs() is called - it shows me error:
bad argument #1 to maxn (table expected, got nil)
My question is: why the table is nil? When it was obtained in main.lua it’s maxn() was 1 - so everything was ok. But inside level1:startLevel() it’s nil - why? Where is my mistake?
[import]uid: 31161 topic_id: 7143 reply_id: 307143[/import]