본문 바로가기
Programming/Android

Cocos2d-x 3.2에서 android back 버튼 처리..

by 신규하 2014. 8. 13.



cocos2d-x 2.x 버전에서는 back 버튼 처리할 때


virtual void keyBackClicked();


이걸 사용해서 처리 했습니다.


하지만, 3.2에서는 keyBackClicked가 final로 처리 되어서..

더 이상 사용 할 수 없습니다.


대신 onKeyReleased 를 사용해야 합니다.


Cocos2d-x에서 기본으로 생성하면 있는 HelloWorldScene에 적용해 보면 아래와 같습니다.


HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
    virtual bool init(); 
    void menuCloseCallback(cocos2d::Ref* pSender);
    CREATE_FUNC(HelloWorld);


    // ###   추가   ####
    void onKeyReleased( cocos2d::EventKeyboard::KeyCode keycode, cocos2d::Event *event );
};

#endif // __HELLOWORLD_SCENE_H__


먼저 헤더에 onKeyReleased를 넣어 주시고..


HelloWorldScene.cpp

#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;
    }
   
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();

    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
   
    closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu, 1);
    auto label = LabelTTF::create("Hello World", "Arial", 24);
    label->setPosition(Point(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));
    this->addChild(label, 1);
    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(sprite, 0);
   

    // ####   추가   ####
    this->setKeypadEnabled( true );

    return true;
}


// ####   추가   ####
void HelloWorld::onKeyReleased( cocos2d::EventKeyboard::KeyCode keycode, cocos2d::Event *event )
{
    if (keycode == EventKeyboard::KeyCode::KEY_BACK) {
        Director::getInstance()->end();
    }
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#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
}


소스코드에서 init 부분에

this->setKeypadEnabled(ture);

를 추가해 줍니다.


그리고, OnKeyReleased 함수를 추가해 줍니다.


이러면 끝~


샘플 소스 다운로드 :

Classes.7z


댓글