How do I loop music and have more than two music in a game?

I am making a simple game where you just shoot astroids. I added music, but when it ends, it does not repeat and the game goes silent. After the music ends, I want a different music to play. After the other music ends, I want it to go back to the first music, and then the other music, and so on, never ending. Please send me the code to do that.

@CoolSheepXX7 You can put the two music names or more if you want in a table and then just loop thru the table as needed.

Or you can simply give each article of music a variable name

m1=music(...) 
m2=music(...)

EDIT: Nevermind, that’s if you want two tracks playing at once.

EDIT: Corrected errors in the code.


function setup()
    tab={"A Hero's Quest:Hero's Loss","A Hero's Quest:Hero's Triumph"}
    parameter.watch("music.duration")
    parameter.watch("music.currentTime")
    play=true
    m=0
end

function draw()
    background(0)
    if play then
        m=m+1
        if m>#tab then
            m=1
        end
        play=false
        music(tab[m])
        c=0
    elseif music.currentTime==0 then
        c=c+1
        if c>20 then
            play=true
        end
    end    
end

It doesn’t say it in the reference but could you give it a return function to call when its finished the track?

@CoolSheepXX7 Here’s another way to play music constantly. Just fill the table with the songs you want, set m=0, and call playMusic().


function setup()
    tab={"A Hero's Quest:Hero's Loss","A Hero's Quest:Hero's Triumph",
            "A Hero's Quest:Battle","A Hero's Quest:Dungeon"}
    parameter.watch("music.duration")
    parameter.watch("music.currentTime")
    m=0
    playMusic()
end

function draw()
    background(0)
end

function playMusic()   
    m=m+1
    if m>#tab then
        m=1
    end
    music(tab[m])
    tween.delay(music.duration+1,playMusic)    
end

When I’m on my mom’s iPad mini coding games, I just use what simply came to my mind. Its acually pretty simple.

music(--Your music here)
if dead == true then
music.stop()
music(--Your other worthless music here)
end

But it’s more for not timing music, but rather music for different scenes.