From c4d787cb2749f6590121d185036ef60c617369c0 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Mon, 7 Dec 2020 01:54:32 +0800 Subject: [PATCH] =?UTF-8?q?task=E6=A8=A1=E5=9D=97=E5=86=8D=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=EF=BC=8C=E4=BF=AE=E5=A4=8Dbgm=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=B2=A1=E8=B7=9F=E4=B8=8Atask=E6=A8=A1=E5=9D=97=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/bgm.lua | 46 +++++++++++++++++------------------------- Zframework/task.lua | 13 ++++-------- parts/scenes/music.lua | 8 +++----- 3 files changed, 25 insertions(+), 42 deletions(-) diff --git a/Zframework/bgm.lua b/Zframework/bgm.lua index 9816008a..48a27fc8 100644 --- a/Zframework/bgm.lua +++ b/Zframework/bgm.lua @@ -1,9 +1,8 @@ local min=math.min -local function fadeOut(id) +local function fadeOut(src) while true do coroutine.yield() - local src=BGM.list[id] local v=src:getVolume()-.025*SETTING.bgm src:setVolume(v>0 and v or 0) if v<=0 then @@ -12,10 +11,9 @@ local function fadeOut(id) end end end -local function fadeIn(id) +local function fadeIn(src) while true do coroutine.yield() - local src=BGM.list[id] local v=SETTING.bgm v=min(v,src:getVolume()+.025*v) src:setVolume(v) @@ -24,10 +22,12 @@ local function fadeIn(id) end end end +local function removeCurFadeOut(task,code,src) + return task.code==code and task.args[1]==src +end local BGM={ --nowPlay=[str:playing ID] - --suspend=[str:pausing ID] --playing=[src:playing SRC] } function BGM.set(L) @@ -52,19 +52,17 @@ function BGM.loadAll() end function BGM.play(s) if SETTING.bgm==0 then + BGM.nowPlay=s BGM.playing=BGM.list[s] - BGM.suspend,BGM.nowPlay=s - return - elseif not(s and BGM.list[s])then return end - if BGM.nowPlay~=s then - if BGM.nowPlay then TASK.new(fadeOut,BGM.nowPlay)end - TASK.changeCode(fadeIn,fadeOut) - TASK.removeTask_data(s) + if s and BGM.list[s]and BGM.nowPlay~=s then + if BGM.nowPlay then TASK.new(fadeOut,BGM.playing)end + TASK.removeTask_iterate(removeCurFadeOut,fadeOut,BGM.list[s]) + TASK.removeTask_code(fadeIn) - BGM.nowPlay,BGM.suspend=s - TASK.new(fadeIn,s) + TASK.new(fadeIn,BGM.list[s]) + BGM.nowPlay=s BGM.playing=BGM.list[s] BGM.playing:play() end @@ -74,23 +72,15 @@ function BGM.freshVolume() local v=SETTING.bgm if v>0 then BGM.playing:setVolume(v) - if BGM.suspend then - BGM.playing:play() - BGM.nowPlay,BGM.suspend=BGM.suspend - end - else - if BGM.nowPlay then - BGM.playing:pause() - BGM.suspend,BGM.nowPlay=BGM.nowPlay - end + BGM.playing:play() + elseif BGM.nowPlay then + BGM.playing:pause() end end end function BGM.stop() - if BGM.nowPlay then - TASK.new(fadeOut,BGM.nowPlay) - end - TASK.changeCode(fadeIn,fadeOut) - BGM.playing,BGM.nowPlay=nil + TASK.removeTask_code(fadeIn) + if BGM.nowPlay then TASK.new(fadeOut,BGM.nowPlay)end + BGM.nowPlay,BGM.playing=nil end return BGM \ No newline at end of file diff --git a/Zframework/task.lua b/Zframework/task.lua index 5d06a259..0e41e0a8 100644 --- a/Zframework/task.lua +++ b/Zframework/task.lua @@ -28,6 +28,7 @@ function TASK.new(code,...) tasks[#tasks+1]={ thread=thread, code=code, + args={...}, } end end @@ -38,17 +39,11 @@ function TASK.newNet(code,...) tasks[#tasks+1]={ thread=thread, code=code, + args={...}, net=true, } end end -function TASK.changeCode(c1,c2) - for i=#tasks,1,-1 do - if tasks[i].thread==c1 then - tasks[i].thread=c2 - end - end -end function TASK.removeTask_code(code) for i=#tasks,1,-1 do if tasks[i].code==code then @@ -56,9 +51,9 @@ function TASK.removeTask_code(code) end end end -function TASK.removeTask_data(data) +function TASK.removeTask_iterate(func,...) for i=#tasks,1,-1 do - if tasks[i].data==data then + if func(tasks[i],...)then rem(tasks,i) end end diff --git a/parts/scenes/music.lua b/parts/scenes/music.lua index 10f34100..88e20891 100644 --- a/parts/scenes/music.lua +++ b/parts/scenes/music.lua @@ -35,10 +35,8 @@ function scene.keyDown(key) end elseif key=="return"or key=="space"then if BGM.nowPlay~=BGM.list[S]then - if SETTING.bgm>0 then - SFX.play("click") - BGM.play(BGM.list[S]) - end + BGM.play(BGM.list[S]) + if SETTING.bgm>0 then SFX.play("click")end else BGM.stop() end @@ -79,7 +77,7 @@ scene.widgetList={ WIDGET.newText{name="now", x=700, y=500,font=50,align="R",hide=function()return not BGM.nowPlay end}, WIDGET.newSlider{name="bgm", x=760, y=80,w=400, font=35,disp=WIDGET.lnk_SETval("bgm"),code=function(v)SETTING.bgm=v BGM.freshVolume()end}, WIDGET.newButton{name="up", x=200, y=250,w=120, font=55,code=WIDGET.lnk_pressKey("up"),hide=function()return sceneTemp==1 end}, - WIDGET.newButton{name="play", x=200, y=390,w=120, font=35,code=WIDGET.lnk_pressKey("space"),hide=function()return SETTING.bgm==0 end}, + WIDGET.newButton{name="play", x=200, y=390,w=120, font=35,code=WIDGET.lnk_pressKey("space")}, WIDGET.newButton{name="down", x=200, y=530,w=120, font=55,code=WIDGET.lnk_pressKey("down"),hide=function()return sceneTemp==BGM.len end}, WIDGET.newButton{name="back", x=1140, y=640,w=170,h=80, font=40,code=WIDGET.lnk_BACK}, }