I've been playing around with figuring out music on the Pico-8. I decided to whip together a quick title screen test. The code is supposed to play a little ditty (sfx(4)) on the title screen but nothing plays until after I press X to go to the next screen. As far as I can tell, code wise that shouldn't be happening. Any idea what's going on here?
function _init()
cls(0)
scene="title"
end
function _update()
if scene=="title" then
update_title()
elseif scene=="instructions" then
update_instructions()
elseif scene=="page2" then
update_page2()
elseif scene=="page3" then
update_page3()
end
end
function _draw()
if scene=="title" then
draw_title()
elseif scene=="instructions" then
draw_instructions()
elseif scene=="page2" then
draw_page2()
elseif scene=="page3" then
draw_page3()
end
end
function update_title()
if btnp(❎) then
scene="instructions"
end
end
function draw_title()
cls(12)
sfx(4)
print("title page",30,45,10)
print("press ❎ to start",30,63)
end
function update_instructions()
if btnp(❎) then
scene="page2"
end
end
function draw_instructions()
cls(3)
print("instructions",40,5,7)
print("page 1",25,45,4)
print("press ❎ to continue",25, 120,7)
end
function update_page2()
if btnp(❎) then
scene="page3"
end
end
function draw_page2()
cls(3)
print("instructions",40,5,7)
print("page 2",25,45,4)
print("press ❎ to continue",25, 120,7)
end
function update_page3()
end
function draw_page3()
cls(3)
print("instructions",40,5,7)
print("page 3",25,45,4)
end



OK. I figured it out. While waiting for x to be pressed to go from the title screen to the instructions screen, the song gets called by sfx(4), the song starts to play but since I have a couple of silent notes at the beginning you don't hear anything, the code loops back around so fast that sfx(4) gets called again before the first audible note is played and instead of playing the rest of the song, it starts over with the silent note again and this just keeps repeating until x is pressed and sfx(4) is no longer being called. This allows the full song to finally play.
adding the following code fixes the issue.
function _init()
cls(0)
scene="title"
startsong = 1 //new code
end
function draw_title()
cls(12)
if startsong == 1 then //new code
sfx(4) //new code
startsong +=1 //new code
end //new code
print("title page",30,45,10)
print("press ❎ to start",30,63)
end
[Please log in to post a comment]