Search code examples
luaandroid-transitionscorona-storyboard

How to move from one lua scene to another without user input like a slide show?


How does one lua file get replaced by another lua file (like a slide show) WITHOUT user input or buttons? End of sound? Timer?

For example, this coding in scene1:

-- visuals

   positionOne = display.newImage( note1, 170, pitch1 ) 
-- first of the two notes in the bar: 170 = x coordinate, pitch = y         

   coordinate
   positionTwo = display.newImage( note2, 320, pitch2 ) 
-- second of the two notes in the bar

-- accomp 1

    local accomp = audio.loadStream("sounds/chord1.mp3")
        audio.play(accomp, {channel = 1})
    audio.stopWithDelay(60000/72)
    -- 72 = beats per minute
-- accomp 2

    local function listener(event)  
    local accomp = audio.loadStream("sounds/chord2.mp3")
        audio.play(accomp, {channel = 2})
    audio.stopWithDelay(60000/72])
    end
    timer.performWithDelay(60000/72, listener)
    end

being succeeded by this once the music has finished:

-- visuals

   positionOne = display.newImage( note1, 170, pitch3 ) 
-- first of the two notes in the bar: 170 = x coordinate, pitch = y         

   coordinate
   positionTwo = display.newImage( note2, 320, pitch4 ) 
-- second of the two notes in the bar

-- accomp 1

    local accomp = audio.loadStream("sounds/chord3.mp3")
        audio.play(accomp, {channel = 1})
    audio.stopWithDelay(60000/72)
    -- 72 = beats per minute
-- accomp 2

    local function listener(event)  
    local accomp = audio.loadStream("sounds/chord4.mp3")
        audio.play(accomp, {channel = 2})
    audio.stopWithDelay(60000/72])
    end
    timer.performWithDelay(60000/72, listener)
    end

As a beginner coder I can't understand Corona's ready-made multi-scene coding which is dependent on user input buttons anyway. I note that on initiating such a project main moves to scene1 directly with no ui. Can this be the case with other scenes? What am I getting wrong?


Solution

  • You can schedule the scene switch with timer.performWithDelay()since you can calculate when the second audio play will stop. Something like:

    local function switchScene()
       composer.gotoScene( "scene2", { effect = "slideLeft", time = 500} )
    end
    
    -- start the audio stuff here as before...
    
    -- Register scene switch to happen once the audio is done
    local timeBeforeSwitch = 2*(60000/72)
    timer.performWithDelay(timeBeforeSwitch, switchScene)
    

    As an alternative it should work to register an onComplete callback to the last audio play call like this:

    audio.play(accomp, {channel = 2, onComplete=switchScene})
    

    But I haven't tested to see that it works as expected with audio.stopWithDelay()