0

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?

julianLE3
  • 85
  • 7
  • possible duplicate of [How do I change scenes in Corona without user input?](http://stackoverflow.com/questions/32337521/how-do-i-change-scenes-in-corona-without-user-input) – ryanpattison Sep 05 '15 at 19:22
  • I asked both questions. As there was no response to the first I assumed I had not actually posted it. The second more clearly explains my problem, though. Hope you can help. – julianLE3 Sep 06 '15 at 11:38
  • you can see what questions you have posted on your profile page. See [How do I get attention for old unanswered questions?](http://meta.stackexchange.com/questions/7046/how-do-i-get-attention-for-old-unanswered-questions) for next time and this time delete your old unanswered question. – ryanpattison Sep 06 '15 at 14:42

1 Answers1

0

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()

johlo
  • 5,422
  • 1
  • 18
  • 32
  • I still can't make one scene switch to another. Is the code you kindly suggest all that is required for one scene or a part of a larger body? Should I be able to repeat this code in scene2, redirecting it back to scene1 in the same way? When I run it I get this error warning: attempt to index global 'composer' (a nil value) stack traceback. Main.lua:10;in function'_listener'. -- main lua 10 is the composer.gotoScene line. – julianLE3 Sep 07 '15 at 19:22
  • I assume you have setup your scene code with something like the template in https://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/ ? One lua file per scene. Then this should work. – johlo Sep 07 '15 at 19:32
  • Thanks, I'll look at that. – julianLE3 Sep 08 '15 at 11:07