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();
    
    }
  • 相关阅读:
    mysql日志
    验证栈序列
    限流方案分析
    集合
    数据结构-树
    链表的中间节点
    PHP实现链表
    php扩展安装方式
    2017 Multi-University Training Contest
    用LCT解一类动态图的问题
  • 原文地址:https://www.cnblogs.com/jiackyan/p/3019853.html
Copyright © 2011-2022 走看看