zoukankan      html  css  js  c++  java
  • 射击小游戏一03(碰撞检测)

    /* 申明数组的时候一定要注意retain:比如  
    
    _targets =  CCArray::create();
    
     _targets->retain();
    
    */
    
     
    
    voidHelloWorld::update(CCTime dt)
    
    {
    
        CCArray *projectilesToDelete = CCArray::create();
    
        projectilesToDelete->retain();
    
        
    
        for (int i = 0; i < _projectiles->count(); i++)
    
        {
    
            CCSprite *projectile = (CCSprite*)(_projectiles->objectAtIndex(i));
    
            CCRect projectileRect = CCRectMake(
    
                                               projectile->getPosition().x
    
                                               - (projectile->getContentSize().width/2),
    
                                               projectile->getPosition().y
    
                                               - (projectile->getContentSize().height/2),
    
                                               projectile->getContentSize().width,
    
                                               projectile->getContentSize().height);
    
            
    
            CCArray *targetsToDelete = CCArray::create();
    
            targetsToDelete->retain();
    
            for (int j = 0; j < _targets->count(); j++)
    
            {
    
                CCSprite *target = (CCSprite *)(_targets->objectAtIndex(j));
    
                CCRect targetRect = CCRectMake(
    
                                               target->getPosition().x - (target->getContentSize().width/2),
    
                                               target->getPosition().y - (target->getContentSize().height/2),
    
                                               target->getContentSize().width,
    
                                               target->getContentSize().height);
    
                
    
              //  if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
    
                if (projectileRect.intersectsRect(targetRect))
    
                {
    
                    targetsToDelete->addObject(target);
    
                }
    
            }
    
            for (int k = 0; k < targetsToDelete->count(); k++)
    
            {
    
                CCSprite *target = (CCSprite *)(targetsToDelete->objectAtIndex(k));
    
                _targets->removeObject(target);
    
                this->removeChild(target, true);
    
            }
    
            
    
            if (targetsToDelete->count() >0)
    
            {
    
                projectilesToDelete->addObject(projectile);
    
            }
    
            targetsToDelete->release();
    
        }
    
        for (int l = 0; l < projectilesToDelete->count(); l++)
    
        {
    
            CCSprite* projectile =  (CCSprite *)(projectilesToDelete->objectAtIndex(l));
    
            _projectiles->removeObject(projectile);
    
            this->removeChild(projectile, true);
    
        }
    
        projectilesToDelete->release();
    
    }
  • 相关阅读:
    【五】服务熔断、降级 —— Hystrix(豪猪)
    32. Springboot 系列(八)动态Banner与图片转字符图案的手动实现
    31.【微服务架构】SpringCloud之Feign(五)
    新SQL temp
    lombok踩坑与思考
    lombok注解介绍
    叉乘实现角色和敌人的位置判断(左上,左下,右上,右下)
    2维,3维向量单位化
    2个2D向量计算交点的夹角和补角
    Unity编辑器-创建单独编辑框,折叠框,提示框
  • 原文地址:https://www.cnblogs.com/jiackyan/p/3019853.html
Copyright © 2011-2022 走看看