cocos2dx 3.2 如何为android平台添加back键的监听

在Scene.h中代码:

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer

{

public:

// there's no 'id' in cpp, so we recommend returning the class instance pointer

static cocos2d::Scene* createScene();



// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone

virtual bool init();



// 添加按键监听

virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);  



// implement the "static create()" method manually

CREATE_FUNC(HelloWorld);

};

<p> </p>
<p></p>
<p>在Scene.cpp中代码:</p>
<p></p>
<pre class="brush:cpp; toolbar: true; auto-links: false;">#include "HelloWorldScene.h"



USING_NS_CC;



Scene* HelloWorld::createScene(){

    auto scene = Scene::create();

    auto layer = HelloWorld::create();

    scene->addChild(layer);

    return scene;

}



// on "init" you need to initialize your instance

bool HelloWorld::init(){

    if ( !Layer::init() ){

        return false;

    }

    

    //注册捕捉监听

    auto listener = EventListenerKeyboard::create();

    listener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);



    return true;

}



void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event){

    switch (keyCode){

    case cocos2d::EventKeyboard::KeyCode::KEY_BACKSPACE:

    case cocos2d::EventKeyboard::KeyCode::KEY_BACK:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)

        MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");

        return;

#endif

        Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

        exit(0);

#endif

        break;

    default:

        break;

    }

}

但是编译无法通过,log为:
1>d:programgametestgameclasseshelloworldscene.h(16): error C2653: “EventKeyboard”: 不是类或命名空间名称 (..ClassesHelloWorldScene.cpp)

1>d:programgametestgameclasseshelloworldscene.h(16): error C2061: 语法错误: 标识符“KeyCode” (…ClassesHelloWorldScene.cpp)

1>d:programgametestgameclasseshelloworldscene.h(16): error C2653: “EventKeyboard”: 不是类或命名空间名称 (…ClassesAppDelegate.cpp)

1>d:programgametestgameclasseshelloworldscene.h(16): error C2061: 语法错误: 标识符“KeyCode” (…ClassesAppDelegate.cpp)

1>d:programgametestgameclasseshelloworldscene.cpp(52): error C2511: “void HelloWorld::onKeyReleased(cocos2d::EventKeyboard::KeyCode,cocos2d::Event *)”:“HelloWorld”中没有找到重载的成员函数

1> d:programgametestgameclasseshelloworldscene.h(6) : 参见“HelloWorld”的声明

<p> </p>
</p>
<p> 求大神帮忙</p>
<p> </p>
<p> </p>

程序写的有问题,back键的监听不是这么搞的

我已经用上面的代码成功的监听到了back键点击事件,问题是.h文件引用了“EventKeyBoard::KeyCode”作为声明函数的参数,需要命名空间“USING_NS_CC”,所以只需要把在.cpp文件中的“USING_NS_CC”这一行剪贴到.h文件中就可以了

为什么我的监听不到 我的控制台输出这个呢

我的代码

auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed,this);

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

auto listener2 = EventListenerKeyboard::create();

listener2->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener2, this);

void HelloWorld::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)

{

if (keyCode == EventKeyboard::KeyCode::KEY_BACK)

{

	MenuItemFont *sureFnt = MenuItemFont::create("Sure");

	MenuItemFont *cancleFnt = MenuItemFont::create("Cancle");

	sureFnt->setPosition(Vec2(200, 400));

	cancleFnt->setPosition(Vec2(400, 400));

	containMnu->addChild(sureFnt,0,"sure");

	containMnu->addChild(cancleFnt,0,"cancle");

	sureFnt->setCallback(CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

	cancleFnt->setCallback(CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));



	CCLOG("you press KEY_BACK key.----------------------------------------------------------------------------------------------------------------------------------------------------------------------------");

}

if (keyCode == EventKeyboard::KeyCode::KEY_HOME)

{

	CCLOG("you press KEY_HOME key.");

}

if (keyCode == EventKeyboard::KeyCode::KEY_BACKSPACE)

{



	MenuItemFont *sureFnt = MenuItemFont::create("SureSpace");

	MenuItemFont *cancleFnt = MenuItemFont::create("CancleSpace");

	sureFnt->setPosition(Vec2(200, 400));

	cancleFnt->setPosition(Vec2(400, 400));

	containMnu->addChild(sureFnt, 0, "sure");

	containMnu->addChild(cancleFnt, 0, "cancle");

	sureFnt->setCallback(CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

	cancleFnt->setCallback(CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

	CCLOG("you press the KEY_BACKSPACE key.");

}



CCLOG("It's in press.***************************************************************************************************************************************************************************************");

}

<p> </p>
</p>

我找到并解决问题了,应该把命名空间(USING_NS_CC;)放在.h文件中,因为.h文件使用了“EventKeyboard::KeyCode”