事件监听
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 代替。
(六)
时间: 2024-10-22 19:17:31