[英]destroy box2d body in cocos2d
我有一個網和一些昆蟲,我正在投擲網,並與昆蟲進行碰撞檢測並將其破壞。 我已經將bod2d用於碰撞檢測。
世界方法
-(void)createWorld
{
// Define the gravity vector.
b2Vec2 b_gravity;
b_gravity.Set(0.0f, -9.8f);
// Do we want to let bodies sleep?
// This will speed up the physics simulation
bool doSleep = true;
// Construct a world object, which will hold and simulate the rigid bodies.
world = new b2World(b_gravity);
world->SetAllowSleeping(doSleep);
world->SetContinuousPhysics(true);
}
建立網站
-(void) createWeb
{
freeBodySprite = [CCSprite spriteWithFile:@"web1.png"];
[self addChild:freeBodySprite z:2 tag:TAG_WEB];
CGPoint startPos = CGPointMake(100, 320/1.25);
bodyDef.type = b2_staticBody;
bodyDef.position = [self toMeters:startPos];
bodyDef.userData = freeBodySprite;
float radiusInMeters = ((freeBodySprite.contentSize.width * freeBodySprite.scale/PTM_RATIO) * 1.0f);
shape.m_radius = radiusInMeters;
fixtureDef.shape = &shape;
fixtureDef.density = 0.01f;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.1f;
circularObstacleBody = world->CreateBody(&bodyDef);
stoneFixture = circularObstacleBody->CreateFixture(&fixtureDef);
freeBody = circularObstacleBody;
}
我用spritesheet創建了昆蟲動畫,然后為該Sprite創建了b2body
-(b2Body *) createMovingBoxObstacle
{
//set this to avoid updating this object in the tick schedule
_ants.userData = (void *)YES;
b2BodyDef bodyDef_Ant;
bodyDef_Ant.type = b2_dynamicBody;
CGPoint startPos = ccp(520,winSize.height/7.6);
bodyDef_Ant.position = [self toMeters:startPos];
bodyDef_Ant.userData = _ants;
b2PolygonShape dynamicBox;
float tileWidth = ((_ants.contentSize.width * _ants.scale/PTM_RATIO) * 0.5f);
float tileHeight = ((_ants.contentSize.height * _ants.scale/PTM_RATIO) * 0.5f);
dynamicBox.SetAsBox(tileWidth, tileHeight);
b2FixtureDef fixtureDef_Ant;
fixtureDef_Ant.shape = &dynamicBox;
fixtureDef_Ant.friction = 0.7;
fixtureDef_Ant.density = 0.1f;
fixtureDef_Ant.restitution = 0.7;
staticBody = world->CreateBody(&bodyDef_Ant);
staticBody->CreateFixture(&fixtureDef_Ant);
VAMovingObstacle* moveableObject = [[VAMovingObstacle alloc] init];
moveableObject.startPoint = ccp(520,winSize.height/7.6);
moveableObject.endPoint = ccp(-50,winSize.height/7.6);
moveableObject.transitionTime = 20.0;
moveableObject.breakTime = 1.0;
moveableObject.obstacleSprite = _ants;
moveableObject.physicalBody = staticBody;
[moveableObject startMovement];
if (!movingObstacles) {
movingObstacles = [[NSMutableArray alloc] init];
}
[movingObstacles addObject:moveableObject];
[moveableObject release];
return staticBody;
}
VAMovingObstacle是一個類,可幫助精靈與b2body一起從左向右移動(運行)
這是我的聯系人列表器EndContact方法
void ContactListener::EndContact(b2Contact* contact)
{
b2Body* bodyA = contact->GetFixtureA()->GetBody();
b2Body* bodyB = contact->GetFixtureB()->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
if (spriteA.tag == TAG_WEB && spriteB.tag >= TAG_ANT) {
}
else if (spriteA.tag >= TAG_ANT && spriteB.tag == TAG_WEB) {
[spriteA removeFromParentAndCleanup:YES];
[[HelloWorldLayer sharedLevel] WebCollisionWithInsect:bodyA];
}
}
}
每當發生碰撞時,EndContact方法都會調用WebCollisionWithInsect:bodyA
-(void) WebCollisionWithInsect:(b2Body*) bodyT{
world->DestroyBody(bodyT);
}
在world-> DestroyBody(bodyT)行上給出錯誤
斷言失敗:(IsLocked()== false),函數DestroyBody,文件/Users/libs/Box2D/Dynamics/b2World.cpp,第134行。
任何想法我在做什么錯。 ?
編輯我已在EndContact中添加此行
destroyCollisionDetectionBody = objBody;
didInsectCollideWithWeb = YES;
保持引用我要破壞的身體。 現在我可以在tick方法中破壞我的身體,如果選中在tick方法中添加了這些行
if(didInsectCollideWithWeb)
{
[self unschedule:@selector(tick:)];
[freeBodySprite removeFromParentAndCleanup:YES]; // this is my web
world->DestroyBody(destroyCollisionDetectionBody);
world->DestroyBody(freeBody);
[self createWeb];
[self schedule:@selector(tick:)];
didInsectCollideWithWeb = NO;
}
但是現在的問題是,當我破壞我的網站時,即自由人。 我無法重新創建它。 就像我以前通過調用[self createWeb]所做的那樣;
您正試圖摧毀一個box2d
對象,而其在模擬階段使用(如EndContact
是在該階段的稱呼)。 仿真階段是調用世界的step
方法時執行的step
。
因此,您應該做的是保留對要刪除的主體對象的引用,然后在box2d模擬階段(在step
方法返回之后)用所需的主體執行WebCollisionWithInsect
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.