Overview :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#ifndef MyTouchListener_H #define MyTouchListener_H #include "cocos2d.h" using namespace cocos2d; class MyTouchListener: public CCNode, public CCTargetedTouchDelegate { public: MyTouchListener(); virtual ~MyTouchListener(); virtual bool ccTouchBegan(CCTouch *touch, CCEvent *event); virtual void ccTouchMoved(CCTouch *touch, CCEvent *event); virtual void ccTouchEnded(CCTouch *touch, CCEvent *event); virtual void ccTouchCancelled(CCTouch *touch, CCEvent *event); virtual void touchDelegateRetain(); virtual void touchDelegateRelease(); }; #endif /* MyTouchListener_H*/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include "MyTouchListener.h" bool MyTouchListener::ccTouchBegan(CCTouch *touch, CCEvent *event) { CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView()); location = this->convertToNodeSpace(location); CCLOG("touch began location f% %f",location.x,location.y); return true; } void MyTouchListener::ccTouchMoved(CCTouch *touch, CCEvent *event) { CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView()); location = this->convertToNodeSpace(location); CCLOG("debug touch moved location f% %f",location.x,location.y); } void MyTouchListener::ccTouchEnded(CCTouch *touch, CCEvent *event) { CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView()); location = this->convertToNodeSpace(location); CCLOG("debug touch end location f% %f",location.x,location.y); } void MyTouchListener::ccTouchCancelled(CCTouch *touch, CCEvent *event) { this->ccTouchEnded(touch, event); } void MyTouchListener::touchDelegateRetain() { this->retain(); } void MyTouchListener::touchDelegateRelease() { this->release(); } MyTouchListener::MyTouchListener() { CCDirector* pDirector = CCDirector::sharedDirector(); //important - add this class as a listener to CCDirector pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true); } MyTouchListener::~MyTouchListener() { CCDirector* pDirector = CCDirector::sharedDirector(); //important - remove this listener from CCDirector pDirector->getTouchDispatcher()->removeDelegate(this); } |
Usage
1 2 3 4 5 6 |
//suppose Test is a CCLayer MyTouchListener* myListener = new MyTouchListener(); this->addChild(myListener); |