Help us understand the problem. What is going on with this article?

cocos2d-x v3.0 betaのタッチイベント

More than 5 years have passed since last update.

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);

間違いなどあれば、ご意見、ご指摘、マサカリ、その他諸々コメントでつけてくれると助かります。

syo-sa1982
Androidアプリ作って生きてる
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away