7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cocos2d-x v3のEventListener登録、解除

Last updated at Posted at 2015-10-16

#タッチイベント
登録

TestNode.cpp
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(TestNode::onTouchBegan, this);
listener->onTouchEnded = CC_CALLBACK_2(TestNode::onTouchEnded, this);
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

解除

getEventDispatcher()->removeEventListenersForTarget(this);

NodeのデストラクタでremoveEventListenersForTarget(this)が呼ばれるため、
通常は自分で解除する必要はない。

#カスタムイベント
登録

_listenerCustom = getEventDispatcher()->addCustomEventListener("test", CC_CALLBACK_1(TestNode::customEventCallback, this));

解除

getEventDispatcher()->removeEventListener(_listenerCustom);

この方法で登録したカスタムイベントリスナーはremoveEventListenersForTargetでは解除されないので、addCustomEventListenerが返すEventListenerCustomのポインタを持っておいて、解除する時に指定する。

または、EventListenerCustomを作ってから、addEventListenerWithSceneGraphPriorityで登録すれば、removeEventListenersForTargetで解除されるようになり、自分で解除する必要がなくなる。

EventListenerCustom* listenerCustom =
EventListenerCustom::create("test2",                                                                    [](EventCustom* event){
    CCLOG("test2");
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerCustom, this);
7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?