The COCOS2D-X genie moves to the clicked location Demo

Source: Internet
Author: User

The effect of this Demo is that when you click the screen with your mouse on your computer or with your fingers on your mobile phone, the corresponding genie will move to the most recent position. 1. First, we should add the genie to be moved and enable the touch screen response during initialization. to enable touch-screen response, you only need to call a function: [cpp] setTouchEnabled (true); // by default, the CCLayer does not respond to touch-screen events. We can simply look at the implementation of this function as follows: [cpp] void CCLayer: setTouchEnabled (bool bEnabled) {if (m_bTouchEnabled! = BEnabled) {m_bTouchEnabled = bEnabled; if (m_bRunning) {if (bEnabled) // register {this-> registerWithTouchDispatcher ();} else {CCDirector :: shareddire()-> getTouchDispatcher ()-> removeDelegate (this) ;}}ii. Rewrite the CCLayer void ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent) function as follows: [cpp] void MainLayer: ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent) {CCSetIterator it = pTouches-> begin (); // Obtain the most recently clicked object CCTouch * pTouch = (CCTouch *) (* it); CCPoint ptLocation = pTouch-> getLocation (); // obtain the coordinate value CCNode * pSprNode = getChildByTag (kTagSprite); // obtain the pointer of the genie to be moved if (pSprNode! = NULL) {pSprNode-> stopAllActions (); // stop all previous actions pSprNode-> runAction (CCMoveTo: create (1, CCPointMake (ptLocation. x, ptLocation. y); // move to the clicked position} 3. Note: ①. CCSetIterator is an iterator. Its definition is as follows: [cpp] typedef std :: set <CCObject *>: iterator CCSetIterator; ②. The structure of the CCSet class is as follows: [cpp] view plaincopyclass CCSet: public CCObject {public: * *************************** private: std :: set <CCObject *> * m_pSet;} PS: because it is basic learning, no analysis is performed. more exciting content will be available in the future. Please pay attention to my solemn statement as follows: 1. This article is from the CDN blog. http://t.cn/z86WXvT Ii. All Rights Reserved. This statement shall not be removed when any individual or website reposts this article. 3. You shall not modify the article unless explicitly stated.

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.