Hey,
I’m creating my first game with Corona and have come acrross a problem which I haven’t been able to solve. Maybe someone can point me to the right direction. I’m using director for changing scenes and ego by Peach for saving level information.
I have 4 levelsx.lua files (levels1, levels2 etc), each of them has 10 level icons which, if pressed, start the corresponding loadx.lua (load1.lua, load2.lua etc) which will initiate levelx.lua (level1.lua, level2.lua etc)
The code for Levels1.lua:
[lua]module(…, package.seeall)
function new()
local localGroup = display.newGroup()
local function checkForFile ()
currentLevel = loadFile (“currentLevel.txt”)
if currentLevel == “empty” then
currentLevel = 1
saveFile(“currentLevel.txt”, currentLevel)
end
end
checkForFile()
local backgroundImage = display.newImageRect( “images/levels.png”, 480, 320 )
backgroundImage.x = 240
backgroundImage.y = 160
localGroup:insert( backgroundImage )
local level = {}
local function goLevel (event)
director:changeScene(event.target.scene)
end
local function setupLevels()
for i = 1, 10 do
if i <=5 then startY = 80 else startY = 150 end
if i <= 5 then startX = i * 70 else startX = (i - 5) * 70 end
if tonumber(currentLevel) >= i then
level[i] = display.newImage(“images/lev”…i…“btn.png”, startX+0.5, startY+0.5 )
localGroup:insert( level[i] )
level[i].scene = “load”…i…""
level[i]:addEventListener(“tap”, goLevel)
elseif tonumber(currentLevel) < i then
level[i] = display.newImage(“images/lev”…i…“btnH.png”, startX+0.5, startY+0.5 )
localGroup:insert( level[i] )
end
end
end
setupLevels()
local introTwoButton
local onIntroTwoTouch = function( event )
if event.phase == “release” and introTwoBtn.isActive then
audio.play( btnSound )
director:changeScene( “levels2”, “crossfade”)
end
end
introTwoBtn = ui.newButton{
defaultSrc = “images/arrowrbtn.png”,
defaultX = 26,
defaultY = 51,
overSrc = “images/arrowrbtn-over.png”,
overX = 26,
overY = 51,
onEvent = onIntroTwoTouch,
id = “IntroTwoButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
introTwoBtn.x = 450
introTwoBtn.y = 150
local closeBtn
local onCloseTouch = function( event )
if event.phase == “release” then
audio.play( tapSound )
director:changeScene( “loadmainmenu”, “crossfade” )
end
end
closeBtn = ui.newButton{
defaultSrc = “images/menubtn.png”,
defaultX = 75,
defaultY = 30,
overSrc = “images/menubtn-over.png”,
overX = 75,
overY = 30,
onEvent = onCloseTouch,
id = “CloseButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
closeBtn.x = 40
closeBtn.y = 300
localGroup:insert( closeBtn )
clean = function()
if btnAnim then transition.cancel( btnAnim ); end
end
return localGroup
end[/lua]
The levels2.lua etc are practically the same, only with a slight different loop in setupLevels()
[lua]local function setupLevels()
for i = 1, 10 do
if i <=5 then startY = 80 else startY = 150 end
if i <= 5 then startX = i * 70 else startX = (i - 5) * 70 end
if tonumber(currentLevel) >= 10+i then
level[10+i] = display.newImage(“images/lev1”…i…“btn.png”, startX+0.5, startY+0.5 )
localGroup:insert( level[10+i] )
– For some reason, works with “level”…10+i… and not with “load1”…i…
level[10+i].scene = “load1”…i…""
level[10+i]:addEventListener(“tap”, goLevel)
elseif tonumber(currentLevel) < 10+i then
level[10+i] = display.newImage(“images/lev1”…i…“btnH.png”, startX+0.5, startY+0.5 )
localGroup:insert( level[10+i] )
end
end
end
setupLevels()[/lua]
The loadx.lua:
[lua]local loadgame = {}
loadgame.new = function( params )
local localGroup = display.newGroup()
local myTimer
local loadingImage
local loadGame = function()
loadingImage = display.newImageRect( “images/loading.png”, 480, 320 )
loadingImage.x = 240; loadingImage.y = 160
local goToGame = function()
director:changeScene( “level1”, “crossfade” )
end
myTimer = timer.performWithDelay( 1000, goToGame, 1 )
end
loadGame()
local initVars = function()
localGroup:insert(loadingImage)
end
clean = function()
if myTimer then timer.cancel( myTimer ); end
end
initVars()
return localGroup
end
return loadgame [/lua]
with the level number changing on each next:
[lua]director:changeScene( “level2”, “crossfade” )[/lua]
Everything works fine in the first levels1.lua (which includes the levels from 1-10) - loadx.lua initializes starting levelx.lua, but with others (levels2, levels3 etc), after pressing on level buttons, only the loading (load11.lua) is displayed and that´s it.
If I add one more level icon (for i=1, 11 do) to the levels1.lua, the loading finishes like in previous ones and level11.lua is loaded.
Even funnier that if I skip the load11.lua or any bigger numbers from
[lua] level[10+i].scene = “load1”…i…""[/lua]
and go directly to level11
[lua]level[10+i].scene = “level1”…i…""[/lua]
all works (level11.lua starts).
So I´m not sure if it is related to ego, director, something else or me being new to Corona and Lua, but would be nice if someone could help.
Thanks,
Gert
[import]uid: 105289 topic_id: 30187 reply_id: 330187[/import]