触碰事件监听器

auto listen = EventListenerTouchOneByOne::create();

listen->onTouchBegan = CC_CALLBACK_2( Hero::onTouchBegan,this);

listen->onTouchMoved = CC_CALLBACK_2( Hero::onTouchMoved, this);

listen->onTouchEnded = CC_CALLBACK_2( Hero::onTouchEnded, this);

listen->onTouchCancelled = CC_CALLBACK_2(Hero::onTouchCancelled, this);

listen->setSwallowTouches(false);

_eventDispatcher->addEventListenerWithFixedPriority(listen,-129);


//这里-129只是标识,可以用对象来做标识,比如sprite,this...这样对象移除,事件监听也会被移除

//事件监听器的分层,决定于注册的对象层次,相同对象则取决于注册先后顺序

//setSwallowTouches决定是否拦截触碰事件,如果为true,则拦截,不往下分发

//移除

Node::_eventDispatcher->removeAllEventListeners();

Node::_eventDispatcher->removeEventListener(xxx);

Node::_eventDispatcher->removeEventListenersForType(xxx);

Node::_eventDispatcher->removeEventListenersForTarget(xxx);



//============================================分割线=========================================================


//添加按钮监听。


MenuItemSprite* pMenuItemBack =addMenuItemSprite("xxx.png",Point::ANCHOR_TOP_LEFT,Point(8,8),CC_CALLBACK_0(GDMJDeskMenu::menuBackCB,this));

newGameItem->setPosition(Point( 40, 20));

newGameItem->setEnabled(true);

auto menu = Menu::create(pMenuItemBack,NULL);

menu->setPosition(Point(0,0));

addChild(menu, GAME_MENU_ORDER_MENU,MJGAME_DESK_MENU_BTN);


//menu按钮监听是该场景最上层,且事件不会下发的。



你可能感兴趣的:(cocos2d)