首页 > 代码库 > (六)

(六)

事件监听

 

    auto listen = EventListenerTouchOneByOne::create();    listen->setSwallowTouches(true);    listen->onTouchBegan = [](Touch* touch,Event* event){        CCLog("onTouchBegan");        return true;    };    listen->onTouchMoved = [](Touch* touch,Event* event){        CCLog("onTouchMoved");    };    listen->onTouchEnded = [](Touch* touch,Event* event){        CCLog("onTouchEnded");    };        Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listen, this);

 

同一个事件监听器可以绑定多个对象,但必须使用事件监听器的复本,如:

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listen, sprite);Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listen->clone(), ttf);

 

 Director::getInstance()->getEventDispatcher() 也可以用 _eventDispatcher 代替。