您好,登錄后才能下訂單哦!
參考文章:
http://labs.easymobi.cn/?p=5167
實現過程,實現CCTouchDelegate接口,在enter 的時候添加監聽,exit 和析構時移除監聽,點擊時將touch 的坐標轉化為相對精靈的坐標,如果是否在精靈的范圍內,則響應點擊。主要是參考前面兩篇文章中的例子,不同的地方在判斷區域,個人覺得太復雜。所以稍微修改了下,看起來更容易理解。
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 Methods PianoTile::~PianoTile() { CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); _delegate = NULL; } #pragma mark Lifecyle void PianoTile::onEnter(){ CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false); CCSprite::onEnter(); } void PianoTile::onExit(){ CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); CCSprite::onExit(); } #pragma mark Touch Delegate bool 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){ }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。