Creating Composer scenes programmatically.

Ok here’s a sample bare bones example. This is contained in a scene called sc1 which is called from main with composer.gotoScene. This produces the above error. [lua] local composer = require( “composer” ) local function dynamicSceneCreator( scene_name ) local scene = composer.newScene( scene_name ) function scene:create( event ) local sceneGroup = self.view end function scene:show( event ) local phase = event.phase if ( phase == “will” ) then print(“will”) elseif ( phase == “did” ) then – remove previous scene’s view if nil~= composer.getScene(“scene_2”) then composer.removeScene(“scene_2”, false) end end end function scene:hide( event ) local phase = event.phase if ( phase == “will” ) then print(“will”) elseif ( phase == “did” ) then print(“did”) end end function scene:destroy( event ) print(“destory”) end – ------------------------------------------------------------------------------- – Listener setup – Scene 1 scene:addEventListener( “create”, scene ) scene:addEventListener( “show”, scene ) scene:addEventListener( “hide”, scene ) scene:addEventListener( “destroy”, scene ) --------------------------------------------------------------------------------- return scene end local function gotoDynamicScene( scene_name ) if composer.getScene( scene_name ) == nil then dynamicSceneCreator( scene_name ) end composer.gotoScene( scene_name ) end gotoDynamicScene(“scene_1”) [/lua]

You don’t need to use scenes in files anymore, you can put that whatever you want, in the main for example. So don’t call it using another composer.gotoScene.

Right thanks. How is the syntax of this code though, all in order?

Got a chance to try this and put everything in main.lua. Works perfectly! Thanks Luisherranz, doing composer this way keeps everything so manageable.

Thank you Luis! Works fine with a random number in scene name.

Do you know if there is a possibility to remove all the ccscene empty files when application starts? It would be great :slight_smile:

No problem.

I don’t know a way right now, but maybe somebody from Corona can have more information about this bug.

No problem  :slight_smile:

Dirty way to delete files :S

local lfs = require( "lfs" ) local path = system.pathForFile( ".." , system.DocumentsDirectory ) if path then lfs.chdir(path) path = lfs.currentdir().."/files/coronaResources" if not lfs.chdir(path) then return end for file in lfs.dir(path) do if string.find( file, ".ccscene" ) ~= nil then local results, reason = os.remove( system.pathForFile( file, path )) end end end

can the ccscene’s be prevented? perhaps try

scene._composerFileName = “”

or

scene._composerFileName = nil

just before returning the dynamic scene

scene._composerFileName = nil works! xD

Thkx! 

Hi.

Added this GitHub repo if someone needs it. It’s a barebone project for dynamic Composer scene creation:

https://github.com/janfma/dynamicComposer

Frode