Cocos2d-x ccsprite genie listen to click events

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.