cocos2d-x之事件传递

时间:2024-09-07 21:34:38

bool HelloWorld::init()

{

if ( !Layer::init() )

{

return false;

}

Size size=Director::getInstance()->getVisibleSize();

auto label=LabelTTF::create("Click me","Courier",30);

label->setPosition(size.width/2,size.height/2);

addChild(label);

auto listener=EventListenerTouchOneByOne::create();

listener->onTouchBegan=[](Touch *t,Event *e){

if (e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLocation())) {

log("OnTouchBegan");

}

return true;

};

listener->onTouchMoved=[](Touch* t,Event *e){

log("onTouchMoved");

};

listener->onTouchEnded=[](Touch* t,Event *e){

log("onTouchEnd");

};

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,label);

return true;

}