只要继承与CCLayer的类都可以实现触摸功能.CCLayer类的触摸事件的一些接口如下:
77 // 单点触碰 78 virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); //开始碰触 79 virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); //碰触移动 80 virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); //碰触结束 81 virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); //碰触取消 82 83 //多点触碰 84 virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); //开始碰触 85 virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent); //碰触移动 86 virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); //碰触结束 87 virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent); //碰触取消
要实现CCLayer的触摸事件,要做三件事:
1.注册触摸事件监听.
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
2.开启触摸事件.
this->setTouchEnabled(true);
3.实现触摸接口.