Cocos2d-x中触摸响应

如果继承之CCLayer

在OnEnter中添加

CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);

 那么该Layer就会响应

bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)

 最后一个参数false表示触摸事件是否继续向优先级低的中传递,false表示要传递,true表示不传递,0表示触摸优先级。

CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);

表示响应

void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)

不论是CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);

还是CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);

在OnExit中都需要

CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);

单点和多点

virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);

virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtuavoid ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

 CCMenu的触摸优先级是-128,值越小,触摸优先级越高

相关推荐