Collision Detection of rectangle in cocos2d-x

Source: Internet
Author: User

# Ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H _ # include "cocos2d. h "using namespace cocos2d; class HelloWorld: public cocos2d: CCLayer {public: virtual bool init (); static cocos2d: CCScene * scene (); CREATE_FUNC (HelloWorld ); virtual void onEnter (); virtual void onExit (); virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent); virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEve Nt); virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent); bool isRectCollision (CCRect rect1, CCRect rect2 );}; # endif/_ HELLOWORLD_SCENE_H _ # include "HelloWorldScene. h "# include" SimpleAudioEngine. h "using namespace CocosDenshion; CCScene * HelloWorld: scene () {CCScene * scene = CCScene: create (); HelloWorld * layer = HelloWorld: create (); scene-> addChild (layer); return scene;} bool HelloWorl D: init () {if (! CCLayer: init () {return false;} CCLayerColor * colorLayer = CCLayerColor: create (ccc4 (0, 0,255,255), 320,480); addChild (colorLayer ); CCLabelTTF * pLabel = CCLabelTTF: create ("~ There is also a collision between meters ~ "," Thonburi ", 24); pLabel-> setColor (ccc3 (255, 0, 0); CCSize size = CCDirector: shareddire()-> getWinSize (); pLabel-> setPosition (ccp (size. width * 0.5, size. height-50); this-> addChild (pLabel, 1,900); CCSprite * sp1 = CCSprite: create ("rect1.png "); sp1-> setPosition (ccp (100,230); addChild (sp1, 0,921); CCSprite * sp2 = CCSprite: create ("rect2.png "); sp2-> setPosition (ccp (240,140); addChild (sp2, 0,922 ); Return true;} bool HelloWorld: isRectCollision (CCRect rect1, CCRect rect2) {float x1 = rect1.origin. x; float y1 = rect1.origin. y; float w1 = rect1.size. width; float h1 = rect1.size. height; float x2 = rect2.origin. x; float y2 = rect2.origin. y; float w2 = rect2.size. width; float h2 = rect2.size. height; if (x1 + w1 * 0.5 return false;} else if (x1-w1 * 0.5> x2 + w2 * 0.5) {return false;} else if (y1 + h1 * 0.5 return False;} else if (y1-h1 * 0.5> y2 + h2 * 0.5) {return false;} return true;} bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {CCSprite * sp1 = (CCSprite *) this-> getChildByTag (921); sp1-> setPosition (pTouch-> getLocation (); return true;} void HelloWorld :: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {CCSprite * sp1 = (CCSprite *) this-> getChildByTag (921); sp1-> setPosition (pTouch-> getLocation ()); CC Sprite * sp2 = (CCSprite *) this-> getChildByTag (922); CCLabelTTF * pLabel = (CCLabelTTF *) this-> getChildByTag (900 ); /// use the custom judgment rectangle collision function // if (this-> isRectCollision (// CCRectMake (sp1-> getPositionX (), sp1-> getPositionY (), sp1-> getContentSize (). width, sp1-> getContentSize (). height), // CCRectMake (sp2-> getPositionX (), sp2-> getPositionY (), sp2-> getContentSize (). width, sp2-> getContentSize (). height) {// pLabel-> se TString ("pull collision! "); //} Else {// pLabel-> setString ("~ No collision yet ~ "); //} // Use the intersectsRect function to detect if (sp1-> boundingBox (). intersectsRect (sp2-> boundingBox () {pLabel-> setString ("collision pull! ");} Else {pLabel-> setString ("~ There is also a collision between meters ~ ") ;}} Void HelloWorld: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {} void HelloWorld: onEnter () {CCDirector: shareddire()-> getTouchDispatcher () -> addTargetedDelegate (this, 0, false); CCLayer: onEnter ();} void HelloWorld: onExit () {CCDirector: sharedDirector ()-> getTouchDispatcher () -> removeDelegate (this); CCLayer: onExit ();}

Related Article

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.