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();
    
    }
  • 相关阅读:
    适配器模式(16)
    状态模式(15)
    用反射技术替换工厂种的switch分支(14)
    2017年目标与规划
    抽象工厂模式(13)
    观察者模式(12)
    建造者模式(11)
    TCP 可靠传输与流量控制的实现
    TCP报文段的首部格式
    TCP可靠传输的工作原理
  • 原文地址:https://www.cnblogs.com/jiackyan/p/3019853.html
Copyright © 2011-2022 走看看