Building a Level Select Scene

I asked this question at the bottom of Rob Miracle’s Tutorial: Building a level Selection Scene but I then I realized it would be more appropriate to ask the question in the forum.   

The tutorial builds level select buttons using widgets (see code below).  I would like to substitute the word “game” for “level1” or “level2” based on the widget button pressed.   I’ve tried several things.  My goal is if level button 1 is pressed it would go to level1.lua or level2.lua etc…

The article is located at:  http://coronalabs.com/blog/2014/08/12/tutorial-building-a-level-selection-scene/


– Button handler to go to the selected level

local function handleLevelSelect( event )

if ( “ended” == event.phase ) then

– ‘event.target’ is the button and ‘.id’ is a number indicating which level to go to.

– The ‘game’ scene will use this setting to determine which level to load.

– This could be done via passed parameters as well.

myData.settings.currentLevel = event.target.id

– Purge the game scene so we have a fresh start

composer.removeScene( “game”, false )

– Go to the game scene

composer.gotoScene( “game”, { effect=“crossFade”, time=333 } ) end end


I’m new to coding so I apologize for my lack of knowledge. 

Thanks,

Lori

:)**** PROBLEM RESOLVED:  I did the following:

–mydata.settings.currentLevel = event.target.id

local levNumber = tonumber(event.target.id)

if levNumber == 1 then

        – Purge the game scene so we have a fresh start

        composer.removeScene( “level1”, false )

        – Go to the game scene

        composer.gotoScene( “level1”, { effect=“crossFade”, time=333 } )

Even easier, and more efficient as you add more levels. 

[lua]

local scene = “level”…levNumber

composer.removeScene(scene,false)

compose.gotoScene(scene,{ effect=“crossFade”, time=333 })

[/lua]

Thank you for the shortcut.  I switched out the code and I received a runtime error - “module scene not found”

Did I overlook something?

OK.  I removed the “” around scene and it worked!!!    THANK YOU SO MUCH!

 local levNumber = tonumber(event.target.id)

local scene = (“level”…levNumber)

        composer.removeScene( scene, false )

        composer.gotoScene( scene, { effect=“crossFade”, time=333 } )

:)**** PROBLEM RESOLVED:  I did the following:

–mydata.settings.currentLevel = event.target.id

local levNumber = tonumber(event.target.id)

if levNumber == 1 then

        – Purge the game scene so we have a fresh start

        composer.removeScene( “level1”, false )

        – Go to the game scene

        composer.gotoScene( “level1”, { effect=“crossFade”, time=333 } )

Even easier, and more efficient as you add more levels. 

[lua]

local scene = “level”…levNumber

composer.removeScene(scene,false)

compose.gotoScene(scene,{ effect=“crossFade”, time=333 })

[/lua]

Thank you for the shortcut.  I switched out the code and I received a runtime error - “module scene not found”

Did I overlook something?

OK.  I removed the “” around scene and it worked!!!    THANK YOU SO MUCH!

 local levNumber = tonumber(event.target.id)

local scene = (“level”…levNumber)

        composer.removeScene( scene, false )

        composer.gotoScene( scene, { effect=“crossFade”, time=333 } )