References:
Cocos2d-x genie listens to click events
Http://labs.easymobi.cn /? P = 5167
The implementation process implements the cctouchdelegate interface, adds a listener when entering, removes the listener when exit and destructor, and converts the touch coordinate to the relative sprite coordinate when you click, if it is within the genie range, the response will be clicked. I personally think it is too complicated to refer to the examples in the previous two articles. So it seems easier to understand it with a slight modification.
Pianotile. h
class PianoTile : public cocos2d::CCSprite,public cocos2d::CCTouchDelegate{public: PianoTile(); ~PianoTile(); //life cycle virtual void onEnter(); virtual void onExit(); //touch virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);};
Pianotile. cpp
#include "PianoTile.h"USING_NS_CC;#pragma mark - Public MethodsPianoTile::~PianoTile(){ CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); _delegate = NULL;}#pragma mark Lifecylevoid PianoTile::onEnter(){ CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false); CCSprite::onEnter();}void PianoTile::onExit(){ CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); CCSprite::onExit();}#pragma mark Touch Delegatebool PianoTile::ccTouchBegan(CCTouch *pTouche, CCEvent *pEvent){ CCPoint touchLocation = pTouche->getLocation(); CCPoint localPoint = convertToNodeSpace(touchLocation); CCRect rect = CCRectMake(0, 0, boundingBox().size.width, boundingBox().size.height); bool isTouched = rect.containsPoint(localPoint); if (isTouched && _delegate) { _delegate->pianoTileClick(this); } return isTouched;}void PianoTile::ccTouchEnded(CCTouch *pTouche, CCEvent *pEvent){ }
This article from the "Walking rain bridge" blog, please be sure to keep this source http://chenjohney.blog.51cto.com/4132124/1433327