/* 申明数组的时候一定要注意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();
}