Getting multiple touch events in cocos2d-x is almost same as getting single touch events.
CCLayer is also already a touch listener, so you can go ahead and override touch methods or touch methods for multiple simultaneous touch.
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 |
#ifndef MULTITOUCHLISTENER_H_ #define MULTITOUCHLISTENER_H_ #include "cocos2d.h" using namespace cocos2d; class MultiTouchListener : public CCLayer { public: virtual bool init(); //methods to get the multiple touch events virtual void ccTouchesBegan(CCSet* touch, CCEvent* event); virtual void ccTouchesMoved(CCSet* touch, CCEvent* event); virtual void ccTouchesEnded(CCSet* touch, CCEvent* event); CREATE_FUNC(MultiTouchListener); MultiTouchListener(); virtual ~MultiTouchListener(); }; #endif /* MULTITOUCHLISTENER_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 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#include "MultiTouchListener.h" bool MultiTouchListener::init(){ if(CCLayer::init()){ return true; }else return false; } void MultiTouchListener::ccTouchesBegan(CCSet* touches, CCEvent* event) { CCSetIterator it = touches->begin(); CCTouch* touch; CCPoint pt; for( int iTouchCount = 0; iTouchCount < touches->count(); iTouchCount++ ) { touch = (CCTouch*)(*it); pt = touch->getLocationInView(); printf( "ccTouchesBegan id:%i %i,%in", touch->getID(), (int)pt.x, (int)pt.y ); it++; } } void MultiTouchListener::ccTouchesMoved(CCSet* touches, CCEvent* event) { CCSetIterator iterator = touches->begin(); CCTouch* touch; CCPoint pt; for( int iTouchCount = 0; iTouchCount < touches->count(); iTouchCount++ ) { touch = (CCTouch*)(*iterator); pt = touch->getLocationInView(); printf( "ccTouchesMoved id:%i %i,%in", touch->getID(), (int)pt.x, (int)pt.y ); iterator++; } } void MultiTouchListener::ccTouchesEnded(CCSet* touches, CCEvent* event) { CCSetIterator iterator = touches->begin(); CCTouch* touch; CCPoint pt; for( int iTouchCount = 0; iTouchCount < touches->count(); iTouchCount++ ) { touch = (CCTouch*)(*iterator); pt = touch->getLocationInView(); printf( "ccTouchesEnded id:%i %i,%in", touch->getID(), (int)pt.x, (int)pt.y ); iterator++; } } MultiTouchListener::MultiTouchListener() { // Touch dispatcher CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0); } MultiTouchListener::~MultiTouchListener() { CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); } |