圆形碰撞

来源:互联网 发布:钢珠自动上弹器在淘宝 编辑:程序博客网 时间:2024/05/17 02:45

新建工程,CircleCollision

修改HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"

using namespace cocos2d;

class HelloWorld :public cocos2d::CCLayer

{

public:

    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)

   virtual bool init();


    // there's no 'id' in cpp, so we recommend to return the class instance pointer

   static cocos2d::CCScene* scene();    

    

    // preprocessor macro for "static create()" constructor ( node() deprecated )

    CREATE_FUNC(HelloWorld);

    

   virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);

   virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);

   virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);

    

   virtual void onEnter();

   virtual void onExit();

    

   bool isCircleCollision(CCPoint pos1,float radius1,CCPoint pos2,float radius2);

    

};


#endif // __HELLOWORLD_SCENE_H__


修改HelloWorldScene.cpp

#include "HelloWorldScene.h"

#include "SimpleAudioEngine.h"


using namespace cocos2d;

using namespace CocosDenshion;


CCScene* HelloWorld::scene()

{

    // 'scene' is an autorelease object

   CCScene *scene = CCScene::create();

    

    // 'layer' is an autorelease object

    HelloWorld *layer =HelloWorld::create();


    // add layer as a child to scene

    scene->addChild(layer);


    // return the scene

   return scene;

}


// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

   if ( !CCLayer::init() )

    {

        return false;

    }


   CCLayerColor *color=CCLayerColor::create(ccc4(255,255, 255, 255),480,320);

   addChild(color);

    

    CCSize size=CCDirector::sharedDirector()->getWinSize();

   CCLabelTTF *pLabel=CCLabelTTF::create("没相撞^.^","Helvetica", 24);

    pLabel->setColor(ccRED);

    pLabel->setPosition(ccp(size.width*0.5,size.height-50));

   this->addChild(pLabel,0,920);

    

    CCSprite *sp1=CCSprite::create("circle1.png");

    sp1->setPosition(ccp(100,170));

   addChild(sp1,0,921);

    

    CCSprite *sp2=CCSprite::create("circle2.png");

    sp2->setPosition(ccp(350,170));

   addChild(sp2,0,922);

    

    return true;

}



boolHelloWorld::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent)

{

   CCPoint touchPoint=pTouch->getLocation();

   CCSprite *sp1=(CCSprite*)this->getChildByTag(921);

    sp1->setPosition(touchPoint);

    return true;

}

voidHelloWorld::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent)

{

   CCPoint touchPoint=pTouch->getLocation();

   CCSprite *sp1=(CCSprite*)this->getChildByTag(921);

   CCSprite *sp2=(CCSprite*)this->getChildByTag(922);

    CCLabelTTF *ttf=(CCLabelTTF*)this->getChildByTag(920);

    if (this->isCircleCollision(sp1->getPosition(), sp1->getContentSize().width*0.5, sp2->getPosition(), sp2->getContentSize().width*0.5)) {

        ttf->setString("相撞啦 =w=||");

    }

   else

    {

        ttf->setString("没相撞^.^");

    }

    sp1->setPosition(touchPoint);

}

voidHelloWorld::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent)

{

}


voidHelloWorld::onEnter()

{

    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0, false);

   CCLayer::onEnter();

}

voidHelloWorld::onExit()

{

    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);

   CCLayer::onExit();

}


boolHelloWorld::isCircleCollision(CCPoint pos1,float radius1,CCPoint pos2,float radius2)

{

   if (sqrt(pow(pos1.x-pos2.x,2)+pow(pos1.y-pos2.y,2))>radius1+radius2) {

        return false;

    }

    return true;

}



原创粉丝点击