Cocos2dx:删除运动体一旦他们离开屏幕

Cocos2dx : Deleting kinematic bodies once they are off screen

本文关键字:他们 离开 屏幕 删除 运动 Cocos2dx      更新时间:2023-10-16

我正在尝试创造一款平台游戏。在我的游戏中,随机平台出现并从屏幕的右侧移动到左侧。

Using :
b2Vec2 impulse = b2Vec2(-5, 0);
platformBody->SetLinearVelocity(impulse);

但是当我看到我的统计数据时,call/countRef(统计数据的第一行)的数量总是随着新平台从右向左移动而增加。我想知道如何才能删除现有的平台,一旦他们已经移动通过左屏幕(屏幕外)。我所有的平台都是b2_kinematic body (Box2D)。

编辑答案:

我通过以下方法解决了这个问题:

for(std::vector::size_type I = 0;= m_platforms.size();我+ +)

{

  if (m_platforms[i]->GetPosition().x < 0.0f && m_platforms[i]->GetType() == b2_kinematicBody) 
    {
      CCSprite *sprite = (CCSprite *) m_platforms[i]->GetUserData();
      sprite->removeFromParentAndCleanup(true);
      world->DestroyBody(m_platforms[i]);
    }
}       

您可以在主循环中检查您的body是否在屏幕之外,如果是,则销毁它们。的东西。

if (platformBody->GetPosition().x < SCREEN_X_LIMIT) {
    world->DestroyBody(platformBody);
    // Also, remove attached sprite if exists
}