cocos2d-x3.0 响应键盘back按键退出游戏

3.0还是有些变化,Layer类的函数要看明白。final过的函数是不允许继承的。

要实现相应back键要这样:

class GameOverLayer : public Layer
{
public:
	GameOverLayer();
	virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) override;
};
void GameOverLayer::onKeyReleased( EventKeyboard::KeyCode keyCode, Event* event )
{
	CC_UNUSED_PARAM(event);
	if(EventKeyboard::KeyCode::KEY_BACKSPACE == keyCode)
	{
		CCLog("back clicked!");
		CCDirector::sharedDirector()->end();
	}
}

GameOverLayer::GameOverLayer()
{
	Layer::init();
	setKeypadEnabled(true);
}

好了,是很简单。

你可能感兴趣的:(cocos2d-x)