1. 程式人生 > >返回按鍵監聽-Quick-Cocos2dx

返回按鍵監聽-Quick-Cocos2dx

按下返回鍵監聽

function FightScene:onKeyboard(code, event)
    if code == cc.KeyCode.KEY_BACK then
        print("按下返回鍵")
        GameApp:enterScene("MainScene");
    end
end

function FightScene:onEnter()
    self.keyListener = cc.EventListenerKeyboard:create()
    --handler(self,self.onKeyboard)用來關聯方法,如果方法直接放在當前位置則可以直接使用無需handler
    self
.keyListener:registerScriptHandler(handler(self,self.onKeyboard),cc.Handler.EVENT_KEYBOARD_RELEASED) local eventDispatch = self:getEventDispatcher() eventDispatch:addEventListenerWithSceneGraphPriority(self.keyListener,self) end