Cocos2d-x genie collision detection (method 1)

Source: Internet
Author: User


Declare the function collision detection function, two genie and rewrite update

Bool isCollision (CCPoint p1, CCPoint p2, int w1, int h1, int w2, int h2); CCSprite * sp2; CCSprite * sp1; virtual void update (float delta ); // override the virtual void registerWithTouchDispatcher (); virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent); virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent ); virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent );

Cpp file implementation
First 2 genie

sp1 = CCSprite::create("5.png");sp1->setScale(0.3);sp1->setPosition(ccp(100, visibleSize.height/2));this->addChild(sp1);sp2 = CCSprite::create("6.png");sp2->setScale(0.3);sp2->setPosition(ccp(300, visibleSize.height/2));this->addChild(sp2);

Enable touch and Timer

setTouchEnabled(true);scheduleUpdate();

Touch the mobile genie sp2 to collide with the genie sp1

Bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {return true;} void HelloWorld: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent ){;} // mobile genie void HelloWorld: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {CCSize winSize = CCDirector: sharedDirector ()-> getVisibleSize (); if (sp2) {CCPoint pos = pTouch-> getDelta (); CCPoint currentPos = sp2-> getPosition (); currentPos = ccpAdd (currentPos, pos); currentPos = ccpClamp (currentPos, CCPointZero, ccp (winSize. width, winSize. height); // you can specify the setPosition (currentPos) of the sprite output screen. // you can call this operation to reset the sprite coordinates.} void HelloWorld: registerWithTouchDispatcher () {CCDirector * pDirector = CCDirector: sharedDirector (); pDirector-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );}

Update function detection collision, isCollision parameter sequence: Coordinates of genie 1, coordinates of genie 2, width and height of genie 1, width and height of genie 2

bool HelloWorld::isCollision( CCPoint p1,CCPoint p2,int w1,int h1,int w2,int h2 ){if(abs(p1.x - p2.x) < w1 + w2 && abs(p1.y - p2.y) < h1 + h2){return true;}return false;}void HelloWorld::update(float delta){if (isCollision(sp1->getPosition(),sp2->getPosition(), 45, 28.3, 47.5, 35)){CCLOG("--------Is Collision, sp2.x: %f, sp2.y: %f", sp2->getPositionX(), sp2->getPositionY());}elseCCLOG("++++++++ Is't Collision, sp2.x: %f, sp2.y: %f", sp2->getPositionX(), sp2->getPositionY());}



Source code: http://download.csdn.net/detail/oyangyufu/7415923


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.