cocos2d-x 碰撞检测

 
#include "HelloWorldScene.h"
#include<ctime>
using namespace cocos2d;
CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
		
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);
		
        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);
	
        // add layer as a child to scene
        scene->addChild(layer);
			
    } while (0);
	
    // return the scene
    return scene;
	
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{

    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

		CCSize  size = CCDirector::sharedDirector()->getWinSize();
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);
        this->addChild(pMenu, 1);
		
        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        CC_BREAK_IF(! pSprite);
        pSprite->setPosition(ccp(size.width/2, size.height/2));
        this->addChild(pSprite, 0);
		

		//
		   //初始化水果
	
		orange =  CCSprite::create("ball.jpg");
		orange->setPosition(ccp(150,300));
		this->addChild(orange,0);
		//移动水果
		CCActionInterval* moveto = CCMoveTo::create(3,ccp(150,0));
		orange->runAction(moveto);
        //
		lanzi = CCSprite::create("lanzi.jpg");
		lanzi->setPosition(ccp(150,0));
		this->addChild(lanzi,0);
        //定时器 获取精灵动态坐标
		this->schedule( schedule_selector(HelloWorld::update) );

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::update(float dt)
{         

	
	CCRect rectorange = CCRectMake(orange->getPosition().x-(orange->getContentSize().width/2)
			                           ,orange->getPosition().y-(orange->getContentSize().height/2)
									   ,orange->getContentSize().width
									   ,orange->getContentSize().height);
CCRect	 rectlanzi = CCRectMake(lanzi->getPosition().x-(lanzi->getContentSize().width/2)
			                           ,lanzi->getPosition().y-(lanzi->getContentSize().height/2)
									   ,lanzi->getContentSize().width
									   ,lanzi->getContentSize().height);
		
		if(rectorange.intersectsRect(rectlanzi))
		{
		orange =  CCSprite::create("ball.jpg");
		orange->setPosition(ccp(200,200));
		this->addChild(orange,0);
			CCLog("YYYYY");
		}
		else
		{
			CCLog("NNNNN");
		}
	 
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
	CCSprite* orange =  CCSprite::create("ball.jpg");
		orange->setPosition(ccp(150,300));
		this->addChild(orange,0);
		//移动水果
		CCActionInterval* moveto = CCMoveTo::create(3,ccp(150,0));
		orange->runAction(moveto);
        //
		CCSprite *lanzi = CCSprite::create("lanzi.jpg");
		lanzi->setPosition(ccp(150,0));
		this->addChild(lanzi,0);
		for(int i=0;i++;i<100){
			Sleep(1000);
	CCRect rectorange = CCRectMake(orange->getPosition().x-(orange->getContentSize().width/2)
			                           ,orange->getPosition().y-(orange->getContentSize().height/2)
									   ,orange->getContentSize().width
									   ,orange->getContentSize().height);
    CCRect	 rectlanzi = CCRectMake(lanzi->getPosition().x-(lanzi->getContentSize().width/2)
			                           ,lanzi->getPosition().y-(lanzi->getContentSize().height/2)
									   ,lanzi->getContentSize().width
									   ,lanzi->getContentSize().height);
		
		if(rectlanzi.intersectsRect(rectorange))
		{
	    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
        CC_BREAK_IF(! pLabel);

        // Get window size and place the label upper. 
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 50));

        // Add the label to HelloWorld layer as a child layer.
        this->addChild(pLabel, 1);
			CCLog("YYYYY");
		}
		else
		{
			CCLog("NNNNN");
		}}
    // "close" menu item clicked
   // CCDirector::sharedDirector()->end();
}

 
 
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值