본문 바로가기

Programming/Cocos2d-x (with. cpp & java )

[cocos2d-x] 스마트폰 Back키 & PC ESC 입력에 따른 화면 전환

code.h

class Test : public Layer{
private:
    bool _eventBack = false; //Android의 Back 혹은 PC의 ESC키 클릭 여부 
    float _maxTime = 30;      //
    int _maxTouch = 2;        //_maxTime 이전까지 클릭해야하는 횟수, 즉 2회 입력을 해야 화면전환이 됨.
    int _eventTouch = 0;
    float _delayTime = 0;   
    
    /* 첫 유저의 입력(Back / ESC)이후 다음 입력까지의 시간) 
     * 유저가 입력(Back버튼 터치 혹은 ESC 입력)함과 동시에 
     * _eventBack가 true가 되면서 _delayTime이 ++되고, 
     * _delayTime이 _maxTime(30)이 되기 전에 유저에게 
     * 입력받는 _eventTouch가 _maxTouch와 동일하거나 더 커야됨.
     * 즉 30이라는 시간 이내에 입력을 2번해야 전환이 됨 */	
     
public:
    void _eventBackTouchListener(Scene* a =NULL);
};

 

code.cpp

void Test::_eventBackTouchListener(Scene* SceneName) 
{
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    
    auto keyinput = EventListenerKeyboard::create();
    
    keyinput->onKeyPressed = ([=](EventKeyboard::KeyCode keycode, Event* evt) 
    {
        if (keycode == EventKeyboard::KeyCode::KEY_BACK) 
        {
            _eventBack = true;
            _eventTouch++;
            if (_eventBack && _eventTouch < _maxTouch) 
            {
                schedule([=](float dt) 
                {
                    _delayTime++;
                    if (_delayTime > _maxTime) {
                        CCLOG("Over 30sec");
                        _eventBack = false;
                        unschedule("_checkBackBtnDelayTime");
                        _delayTime = 0;
                        _eventTouch = 0;
                    }
                }, "_checkBackBtnDelayTime");
            } 
            else 
            {
                unschedule("_checkBackBtnDelayTime");
            }
        }
    });
    keyinput->onKeyReleased = ([=](EventKeyboard::KeyCode keycode, Event* evt) 
    {
        if (keycode == EventKeyboard::KeyCode::KEY_BACK) 
        {
            _eventBack = false;
            if (_eventTouch >= _maxTouch) 
            {
                if (SceneName == NULL)    Director::getInstance()->end();
                else                    Director::getInstance()->replaceScene(TransitionFade::create(0.5, SceneName));
            }
        }
    });
    _eventDispatcher->addEventListenerWithSceneGraphPriority(keyinput, this);
    
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

    auto keyinput = EventListenerKeyboard::create();
    keyinput->onKeyPressed = ([=](EventKeyboard::KeyCode keycode, Event* evt) 
    {
        if (keycode == EventKeyboard::KeyCode::KEY_ESCAPE) 
        {
            _eventBack = true;
            _eventTouch++;
            if (_eventBack && _eventTouch < _maxTouch) {
                schedule([=](float dt) {
                    _delayTime++;
                    if (_delayTime > _maxTime) {
                        CCLOG("Over 30sec");
                        _eventBack = false;
                        unschedule("_checkBackBtnDelayTime");
                        _delayTime = 0;
                        _eventTouch = 0;
                    }
                }, "_checkBackBtnDelayTime");
            } 
            else 
            {
                unschedule("_checkBackBtnDelayTime");
            }
        }
    });
    keyinput->onKeyReleased = ([=](EventKeyboard::KeyCode keycode, Event* evt) 
    {
        if (keycode == EventKeyboard::KeyCode::KEY_ESCAPE) 
        {
            _eventBack = false;
            if (_eventTouch >= _maxTouch) 
            {
                if(SceneName == NULL )    Director::getInstance()->end();
                else                    Director::getInstance()->replaceScene(TransitionFade::create(0.5, SceneName));
            }
        }
    });
    _eventDispatcher->addEventListenerWithSceneGraphPriority(keyinput, this);
#endif // !CC_PLATFORM_WIN32
}