cocos2d x - C++ cocos2dx event callback called multiple times -


yo! have small experiment in have many touchable blocks. once touched, blocks must change properties, size, color , opacity. when touch 1 block, callbacks assigned other blocks fired. how supposed use cocos2dx's event dispatcher mechanism in order 1 callback fired (the one, , 1 that's assigned it)? in other words, if click block a, ontouchbegin callback assigned fired. here's actual code:

bool simplegamelayer::init() {    // ...    // begin loop here    auto square = drawnode::create();   square->setcontentsize( size(square_size, square_size) );   // ...   square->drawsolidpoly(verts, 4, color4f(0x8b/255.0, 0xb7/255.0, 0xc4/255.0, 1.0f));   addchild(square, 1);    // ...    auto listener = eventlistenertouchonebyone::create();   listener->setswallowtouches( true );   listener->ontouchbegan = [](touch* touch, event* event) {             auto target = static_cast<drawnode*>(event->getcurrenttarget());             log("touchbegan on %s", target->getname().c_str());             return false;         };    _eventdispatcher->addeventlistenerwithscenegraphpriority(listener, square);    // ...    // end loop here } 

no matter block or square touch (or click) bunch of lines:

touchbegan on .... touchbegan on .... touchbegan on .... touchbegan on .... touchbegan on .... 

i tested code adding squares different priorities no avail.

because ontouchbegin allways return false, couldn't swallow touch. if want 1 block touch event,you need change ontouchbegin callback list this.

listener->ontouchbegan = [](touch* touch, event* event) {             auto target = static_cast<drawnode*>(event->getcurrenttarget());             log("touchbegan on %s", target->getname().c_str());             return true;         }; 

Comments