Lua/Corona暂停按钮在按钮外部工作。

有人有想法,为什么我的暂停按钮不起作用?...如果您单击屏幕上的任何位置,它会暂停,而应该只限于按键。

代码:

function pauseIt(event)
    if event.phase == "began" then
        if paused == false then
            audio.play(sound_pause)
             physics.pause()
             paused = true
        elseif paused == true then

             physics.start()
             audio.play(sound_pause)
             paused = false
        end
   end
end

paused = false
Runtime:addEventListener("touch", pauseIt)

   pause = display.newImage( screenGroup, "pause.png" )
    pause.x = _W * 0.12
    pause.y = _H * 0.06
    pause.xScale = 0.2
    pause.yScale = 0.2
    pause.touch = pauseIt
点赞
用户869951
用户869951

你的代码中有 pause.touch = pauseIt 这一句,它会在按钮被触摸时调用 pauseIt(event) ,这很好。但是你同时也为整个场景添加了触摸事件处理函数,通过 Runtime:addEventListener("touch", pauseIt) 。请移除它。

你还需要让按钮监听 tap 事件:

pause:addEventListener("tap", pauseIt)

Corona 文档写得很好(例如 http://developer.coronalabs.com/content/application-programming-guide-tutorial-introduction#Basic_Interactivity) 。

2014-01-16 20:40:46