cocos2d-3.9
用 HelloWorldScene.h 和 HelloWorldScene.cpp 举例
用 HelloWorldScene.h 和 HelloWorldScene.cpp 举例
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
//注意这个,cocos2d-x-3.9\tests\cpp-tests\Classes 目录下寻找此文件和cpp文件导入
#include "VisibleRect.h"
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
//增加这个方法用来处理按键事件
void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
....
//添加按键监听(下面3行放到 init 方法中,放到createScene会报错,具体没看)
auto listener = EventListenerKeyboard::create();
listener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
....
//下面的方法 放到文件最后就可以
//按键控制(检测onKeyReleased有反应)
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
switch(keyCode)
{
//监听返回键
case EventKeyboard::KeyCode::KEY_ESCAPE:
Director::getInstance()->end();
break;
//监听menu键
case EventKeyboard::KeyCode::KEY_MENU:
break;
}
}