2015년 12월 29일 화요일

corona sdk에서 취소키 처리


corona sdk에서 취소키를 누르면 곧장 런처로 빠져나와서 여간 사용이 불편합니다.
아래 링크를 참고해서 back 키 핸들러를 구현을 하면 됩니다.

https://docs.coronalabs.com/api/event/key/keyName.html


Example

-- Called when a key event has been received
local function onKeyEvent( event )
    -- Print which key was pressed down/up
    local message = "Key '" .. event.keyName .. "' was pressed " .. event.phase
    print( message )

    -- If the "back" key was pressed on Android or Windows Phone, prevent it from backing out of the app
    if ( event.keyName == "back" ) then
        local platformName = system.getInfo( "platformName" )
        if ( platformName == "Android" ) or ( platformName == "WinPhone" ) then
            return true
        end
    end

    -- IMPORTANT! Return false to indicate that this app is NOT overriding the received key
    -- This lets the operating system execute its default handling of the key
    return false
end

-- Add the key event listener
Runtime:addEventListener( "key", onKeyEvent )

여기서 주의해야 할점은  return 값입니다. 처리를 했다면 return true를 넘겨줘서 시스템의 취소키 처리를 안하도록 해야합니다.
아래처럼 구현하였습니다. isPause는 cbBtnPause가 지속적으로 호출되는것을 막기위해서 사용되었습니다.



local function cbBtnPause( event )
 playSound("click")
 removeAllListener()
 isPause = true
 loadShadowPopup()
 pauseGroup = display.newGroup()
 panelPause = display.newImage(pauseGroup,"resource/panelpause.png",centerX,centerY)
 btnResume = display.newImage(pauseGroup,"resource/btnresume.png",centerX,centerY-100+10)
 btnResume:addEventListener("touch", resumeProcess)

 btnRestart = display.newImage(pauseGroup,"resource/btnrestart.png",centerX,centerY+10)
    btnRestart.destination = "autoloadlevelplay"
 btnRestart:addEventListener("touch", buttonHit) 

 btnExit = display.newImage(pauseGroup,"resource/btnexit.png",centerX,centerY+100+10)
    btnExit.destination = "menu"
 btnExit:addEventListener("touch", buttonHit) 

 sceneGroup:insert(pauseGroup)
end


local function onKeyEvent( event )
 local phase = event.phase
 local keyName = event.keyName

 if( isPause == true ) then
  return true
 end
 if( keyName == "back") then
  cbBtnPause()
  return true
 end
 return false
end


 Runtime:addEventListener( "key", onKeyEvent )

댓글 없음:

댓글 쓰기