Cocos2d-x genie mobile

Source: Internet
Author: User

In HelloWorldScene. declare class HelloWorld: public cocos2d: CCLayer {public :...... CCPoint convertToGL (CCSet * pTouches); virtual void ccTouchesMoved (CCSet * pTouches, CCEvent * event); private: CCSprite * m_pSprite;}; In HelloWorldScene. make the following declaration in cpp // on "init" you need to initialize your instancebool HelloWorld: init () {bool bRet = false; do {CC_BREAK_IF (! CCLayer: init ());...... // MYCode m_pSprite = CCSprite: create ("luffy.png"); // CCSize size = CCDirector: shareddire()-> getWinSize (); // note m_pSprite-> setPosition (ccp (size. width/2, size. height/2); this-> addChild (m_pSprite, 1); // note this-> setTouchEnabled (true); bRet = true;} while (0 ); return bRet;} CCPoint HelloWorld: convertToGL (CCSet * pTouches) {if (pTouches) {CCSetIterator it = pTouche S-> begin (); CCTouch * touch = (CCTouch *) (* it); CCPoint m_tBeginPos = touch-> locationInView (); m_tBeginPos = CCDirector: shareddire () -> convertToGL (m_tBeginPos); return m_tBeginPos;} assert ("pTouches is NULL");} void HelloWorld: ccTouchesMoved (CCSet * pTouches, CCEvent * event) {CCPoint touch_pos = convertToGL (pTouches); CCPoint cur_pos = m_pSprite-> getPosition (); if (ccpDistance (touch_pos, cur_pos )! = 0) {int dx = touch_pos.x-cur_pos.x; int dy = touch_pos.y-cur_pos.y; CCPoint vector = CCPoint: CCPoint (dx, dy ); double dist = sqrt (dx * dx + dy * 1.0); // note CCPoint unit_vector = CCPoint: CCPoint (dx/dist, dy/dist ); int speed = 1; m_pSprite-> setPosition (ccp (cur_pos.x + unit_vector.x * speed, cur_pos.y + unit_vector.y * speed ));}} the effect implemented by the program is that sprite will move closer to the location of the mouse at the specified speed

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.