Troubles switching scenes on event!

Hey guys so I need help switching scenes I got all the code in scene one and it works but when I try to switch to scene 2 on a certain event I get and error I will post my code below. (I took the code from the composer example)

I don’t know if I am doing it right or not? The error is (error loading module  ‘scene2’ from scene2.lua)

Any help thanks!

Scene1.lua

_local sceneName = …

local composer = require( “composer” )

– Load scene with same root filename as this file
local scene = composer.newScene( sceneName )


function scene:create( event )
    local sceneGroup = self.view
    local physics = require(“physics”)
    physics.start()
    – Called when the scene’s view does not exist
    –
    – INSERT code here to initialize the scene
    – e.g. add display objects to ‘sceneGroup’, add touch listeners, etc
end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

    if phase == “will” then
        – Called when the scene is still off screen and is about to move on screen
        elseif phase == “did” then
        – Called when the scene is now on screen
        –
        – INSERT code here to make the scene come alive
        – e.g. start timers, begin animation, play audio, etc
        
        – we obtain the object by id from the scene’s object hierarchy
            local score = require( “score” )

      local scoreText = score.init({
      fontSize = 20,
      font = “Helvetica”,
      x = 25,
      y = 00,
      maxDigits = 4,
      leadingZeros = true,
      filename = “score.lua”,
      })
      scoreText:setFillColor(1,0,0)     

            local numberSmiles = 1 --local variable; amount can be changed
     

        local function spawnSmiles()

           for i=1,numberSmiles do
              local smile = display.newImageRect(“parachuteguy.png”, 45, 45);
              – smile:setReferencePoint(display.CenterReferencePoint);  --not necessary; center is default
              smile.x = math.random(-10, 400);
              smile.y = -40;
              transition.to( smile, { time=math.random(2000,8000), x=math.random(-10,400) , y=600, onComplete=clearSmile } );              

              physics.addBody( smile, “dynamic”, { density=0.1, bounce=0.1, friction=0.1, radius=0 } );

              —scale the parachute actor                                                                  --Adding touch event
              smile:scale(3,5)
                                                                                local function onObjectTouch( event )
                                                                                    if event.phase == “began” then
                                                                                        print( "Touch event began on: " )
                                                                                        smile:removeSelf()
                                                                                        score.add(event.target.value)
                                                                                        print(math.random(10))
                                                                                    end
                                                                                    return true
                                                                                end
                                                                                smile:addEventListener( “touch”, onObjectTouch )

           end

        end

        timer.performWithDelay( 1000,spawnSmiles, 0 )  --fire every second
                                  local timeLimit = 20
          timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 14)
          timeLeft:setTextColor(255,0,0)
          local function timerDown()
             timeLimit = timeLimit-1
             timeLeft.text = timeLimit
               if(timeLimit==0)then
                 composer.gotoScene(“scene2”)
                  print(“Time Out”) – or do your code for time out
               end
            end
          timer.performWithDelay(1000,timerDown,timeLimit)

    end    
    

       

    
end

function scene:hide( event )
    local sceneGroup = self.view
    local phase = event.phase

    if event.phase == “will” then
        – Called when the scene is on screen and is about to move off screen
        –
        – INSERT code here to pause the scene
        – e.g. stop timers, stop animation, unload sounds, etc.)
    elseif phase == “did” then
        – Called when the scene is now off screen
    end
end

function scene:destroy( event )
    local sceneGroup = self.view

    – Called prior to the removal of scene’s “view” (sceneGroup)
    –
    – INSERT code here to cleanup the scene
    – e.g. remove display objects, remove touch listeners, save state, etc
end


– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene_

 

scene2.lua

_local sceneName = scene2

local composer = require( “composer” )

– Load scene with same root filename as this file
local scenecomposer.newScene( scene2 )


function scene:create( event )
    local sceneGroup = self.view
      
    – Called when the scene’s view does not exist
    –
    – INSERT code here to initialize the scene
    – e.g. add display objects to ‘sceneGroup’, add touch listeners, etc
end

function scene:show( event )
    local sceneGroup = self.view
 
              local testText = display.newText(“This is Scene2”, 160, 20, native.systemFontBold, 14)

end

function scene:hide( event )
    local sceneGroup = self.view
    local phase = event.phase

    if event.phase == “will” then
        – Called when the scene is on screen and is about to move off screen
        –
        – INSERT code here to pause the scene
        – e.g. stop timers, stop animation, unload sounds, etc.)
    elseif phase == “did” then
        – Called when the scene is now off screen
    end
end

function scene:destroy( event )
    local sceneGroup = self.view

    – Called prior to the removal of scene’s “view” (sceneGroup)
    –
    – INSERT code here to cleanup the scene
    – e.g. remove display objects, remove touch listeners, save state, etc
end


– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene_

 

Important notes about Composer:

1.) You should be creating objects in the scene:create function. This function is called prior to the scene actually being on the screen. If you create your objects in scene:show, the scene is actually already on the screen, so there will be a noticeable and undesirable effect of the objects popping on the screen.

2.) Display objects need to be inserted into the sceneGroup. Composer handles this group’s removal when you transition between scenes. If you do not insert your objects into this group, when you change scenes your objects will remain on the screen.

3.) Local variables, requires and forward declarations should be done outside of the scene functions, preferably at the top of your code, so they don’t get lost in the shuffle. Don’t require physics inside your scene:create function, require it right below where you require the composer api.

4.) The problem with scene2, aside from the things mentioned above is this line of code:

local scenecomposer.newScene( scene2 )

@James Sherburne How do I fix it for scene2

local composer = require( "composer" ) local scene = composer.newScene() --------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view -- Called when the scene's view does not exist local testText = display.newText("This is Scene2", 160, 20, native.systemFontBold, 14) sceneGroup:insert(testText) -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc end function scene:show( event ) local sceneGroup = self.view end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

@tpjacobson01,

Take a look at this. First look at the way the scene is declared at the top. You don’t need to name your scenes within the code. Simply putting:

local scene = composer.newScene()

Tells Corona that this lua file is a new composer scene. To get to this scene from any other scene you would use:

composer.gotoScene("scene2")

The Composer API knows to look for the file scene2.lua.

Also look at scene:create function. You will see that I moved your text object out of scene:show and moved it to scene:create. Also notice that after declare the text object I inserted it into sceneGroup. This is absolutely a must if you want to successfully use Composer. Any time you have a display object, make sure you put it into that sceneGroup the way I did. Keep in mind when doing this that display objects are put on the screen in the order you place them in the sceneGroup. So, if you put a bunch of display objects into the sceneGroup and the last one is your background that takes up the whole screen, it will cover everything else up. So make sure you know what order you want things displayed.

Edit: I would suggest going back through your first scene and making these changes.

Important notes about Composer:

1.) You should be creating objects in the scene:create function. This function is called prior to the scene actually being on the screen. If you create your objects in scene:show, the scene is actually already on the screen, so there will be a noticeable and undesirable effect of the objects popping on the screen.

2.) Display objects need to be inserted into the sceneGroup. Composer handles this group’s removal when you transition between scenes. If you do not insert your objects into this group, when you change scenes your objects will remain on the screen.

3.) Local variables, requires and forward declarations should be done outside of the scene functions, preferably at the top of your code, so they don’t get lost in the shuffle. Don’t require physics inside your scene:create function, require it right below where you require the composer api.

4.) The problem with scene2, aside from the things mentioned above is this line of code:

local scenecomposer.newScene( scene2 )

@James Sherburne How do I fix it for scene2

local composer = require( "composer" ) local scene = composer.newScene() --------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view -- Called when the scene's view does not exist local testText = display.newText("This is Scene2", 160, 20, native.systemFontBold, 14) sceneGroup:insert(testText) -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc end function scene:show( event ) local sceneGroup = self.view end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

@tpjacobson01,

Take a look at this. First look at the way the scene is declared at the top. You don’t need to name your scenes within the code. Simply putting:

local scene = composer.newScene()

Tells Corona that this lua file is a new composer scene. To get to this scene from any other scene you would use:

composer.gotoScene("scene2")

The Composer API knows to look for the file scene2.lua.

Also look at scene:create function. You will see that I moved your text object out of scene:show and moved it to scene:create. Also notice that after declare the text object I inserted it into sceneGroup. This is absolutely a must if you want to successfully use Composer. Any time you have a display object, make sure you put it into that sceneGroup the way I did. Keep in mind when doing this that display objects are put on the screen in the order you place them in the sceneGroup. So, if you put a bunch of display objects into the sceneGroup and the last one is your background that takes up the whole screen, it will cover everything else up. So make sure you know what order you want things displayed.

Edit: I would suggest going back through your first scene and making these changes.