使用Tap Detection或Touch Detection都可以,
但如果想偵測雙擊的動作,那使用tap detection會簡單些,
範例如下:
local function buttonListener( event ) if ( event.numTaps == 1 ) then print( "single-tapped" ) runSingleTappedAction() elseif ( event.numTaps == 2 ) then print( "double-tapped" ) runDoubleTappedAction() else return true end end local myButton = display.newRect( 160, 240, 120, 60 ) myButton:addEventListener( "tap", buttonListener )雙擊時,一定是先發生單擊事件,然後才是雙擊事件,
也就是說,會執行runSingleTappedAction(),然後才是runDoubleTappedAction(),
如果雙擊時,不想執行runSingleTappedAction(),
那就必需等一段時間確認是單擊或雙擊再執行,
如以下用法:
local tapCount=0 local timeId = nil function runTapAction( event ) if ( tapCount == 1 ) then print( "single-tapped" ) runSingleTappedAction() elseif ( tapCount == 2 ) then print( "double-tapped" ) runDoubleTappedAction() end end local function buttonListener( event ) tapCount = event.numTaps if(timeId ~= nil) then timer.cancel( timeId ) end timeId = timer.performWithDelay( 200, runTapAction,1 ) end local myButton = display.newRect( 160, 240, 120, 60 ) myButton:addEventListener( "tap", buttonListener )當然,以上的作法會犠牲單擊的反應速度
沒有留言:
張貼留言