본문 바로가기

Game/Cocos2d-x

[Cocos2d-x] v3.0 click event

cocos2d-x v3.0에서 터치 이벤트를 구현하는 방법이 바뀐 위에 

싱글 터치 멀티 터치 모두 갖추어 진 정보가 적었 기 때문에 참고. 


어디 까지나 예문이므로, 전후의 코드 라든지 생략...

싱글 터치 

헤더 파일

Hoge.h
    bool onTouchBegan(Touch* touch, Event* event);
    void onTouchMoved(Touch* touch, Event* event);
    void onTouchEnded(Touch* touch, Event* event);
    void onTouchCancelled(Touch* touch, Event* event);

Cpp 파일

Hoge.cpp
    auto dispatcher = Director::getInstance()->getEventDispatcher();
    auto listener = EventListenerTouchOneByOne::create();

    listener->onTouchBegan = CC_CALLBACK_2(Hoge::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(Hoge::onTouchMoved, this);
    listener->onTouchEnded = CC_CALLBACK_2(Hoge::onTouchEnded, this);
    listener->onTouchCancelled = CC_CALLBACK_2(Hoge::onTouchCancelled, this);

    dispatcher->addEventListenerWithSceneGraphPriority(listener, this);

멀티 터치

헤더 파일

Hoge.h
    virtual void onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *pEvent);
    virtual void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches,cocos2d::Event *pEvent);
    virtual void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *pEvent);
    virtual void onTouchesCancelled(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *pEvent);

Cpp 파일

Hoge.cpp
    auto dispatcher = Director::getInstance()->getEventDispatcher();
    auto listener = EventListenerTouchAllAtOnce::create();

    listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
    listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
    listener->onTouchesCancelled = CC_CALLBACK_2(HelloWorld::onTouchesCancelled, this);
    listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);

    dispatcher->addEventListenerWithSceneGraphPriority(listener, this);

출처 : http://qiita.com/syo-sa1982/items/f6ecf4b6c2f203930eff