cocos2dx 矩形碰撞

bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    CCLayerColor* colorLayer = CCLayerColor::create(ccc4(255, 0, 255, 0),480   ,320);

    
    addChild(colorLayer);
 
    CCLabelTTF * pLabel = CCLabelTTF::create("~还没有碰撞·~", "", 20);
    pLabel->setPosition(ccp(250,300 ));
    pLabel->setColor(ccc3(255, 255, 0));
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    addChild(pLabel,1,900);
    
    CCSprite * sp1 =CCSprite::create("rect1.png");
    sp1->setPosition(ccp(250,200));
    addChild(sp1,0,921);

    CCSprite * sp2 =CCSprite::create("rect2.png");
    sp2->setPosition(ccp(250,100));
    addChild(sp2,0,922);
    return true;
}

  void HelloWorld:: onEnter()
{
    CCDirector ::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
    CCLayer::onEnter();
}
  void HelloWorld::onExit()
{
    CCDirector ::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
    CCLayer::onExit();
    
}
  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());
    
    CCSprite * sp2 = (CCSprite*)this->getChildByTag(922);
    CCLabelTTF * pLabel = (CCLabelTTF*)this->getChildByTag(900);
    
//    利用自定义判断矩形碰撞函数
//    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->setString("碰撞了!");
//    } else {
//        pLabel->setString("~还没有碰撞·~");
//    
//    }
    
//    利用intersectsrect 函数检测碰撞
    if (sp1->boundingBox().intersectsRect(sp2->boundingBox())) {
         pLabel->setString("碰撞了!");    
    }else
        pLabel->setString("~还没有碰撞·~");
 
}
  void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
    CCLOG("GAME OVER");
}

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<x2-w2*0.5)  
        return false;
    else if (x1-w1*0.5>x2+w2*0.5)
        return false;
    else if (y1+h1*0.5<y2-h2*0.5)
        return false;
    else if (y1-h1*0.5>y2+h2*0.5)
        return false;
    
    return true;
}

 

转载于:https://www.cnblogs.com/linux-ios/archive/2013/04/13/3018742.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值