我现在的删除方法是这样的,
world->Step(dt, VELOCITY_ITERATI, POSITION_ITERATI);
std::vectortoDestroy; //--备份需要删除的刚体
for(b2Body* b = world->GetBodyList(); b != NULL ; b = b->GetNext())
{
if (b->getState() == b2_stateRemoved)
{
continue;
}
if (b->GetUserData() != NULL)
{
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite* myActor = (CCSprite*)b->GetUserData();
//--刚体的运动决定精灵的位置
myActor->setPosition(ccp(b->GetPosition().x * sGlobal->ptmRatio,
b->GetPosition().y * sGlobal->ptmRatio));
switch (myActor->getTag())
{
case tagBarDown:
{
if (!b->getPath())
{
if (b->GetPosition().x * sGlobal->ptmRatio < visibleSize.width / 3)
{
b->setPath(true);
score++;
//--music
playSounds(SFX_POINT);
}
}
if (b->GetPosition().x * sGlobal->ptmRatio < -(myActor->getContentSize().width / 2))
{
b->setState(b2_stateRemoved);
toDestroy.push_back(b);
}
}
break;
case tagBarUp:
{
if (b->GetPosition().x * sGlobal->ptmRatio < -(myActor->getContentSize().width / 2))
{
b->setState(b2_stateRemoved);
toDestroy.push_back(b);
}
}
break;
case tagRoad:
{
//--地面滚动
if (myActor->boundingBox().getMaxX() <= 0) // 如果完全消失在屏幕上,就移动精灵1到精灵2的后面
{
b->SetTransform(b2Vec2(visibleSize.width * 3 / 2 / sGlobal->ptmRatio,
_road->getContentSize().height / 2 / sGlobal->ptmRatio),
myActor->getRotation() / 2 / sGlobal->ptmRatio);
}
}
break;
default:
break;
}
}
}
//--删除
std::vector::iterator pos2;
for (pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2)
{
b2Body* body = *pos2;
if(body->GetUserData()!=NULL)
{
CCSprite* sprite = (CCSprite*)body->GetUserData();
this->removeChild(sprite,true);
}
world->DestroyBody(body);
body = NULL;
}
这个方法写在 update 中, 然后发现屏幕会有明显的卡顿。游戏是为了实现 flappyBird 的,求高手给予指导。