我正在为我的游戏使用spacemunager和花栗鼠物理。 我想做的是将一个球击到一个支柱并让球消失。我正在不同的位置重新创建球,以便用户可以击中另一个支柱。我有球形和球精灵作为成员变量。
以下是创建球形,柱形等的代码。
ball = [smgr addCircleAt:cpv(1000,10) mass:0.5 radius:35];
ball->collision_type = kBallCollisionType;
ballSprite = [cpCCSprite spriteWithShape:ball file:@"head.png"];
//[ballSprite autoFreeShape];
[self addChild:ballSprite];
ballSprite.spaceManager = smgr;
//ballSprite.ignoreRotation = NO;
cpShape *dome = [smgr addRectAt:cpv(400,500) mass:1 width:400 height:100 rotation:0];
dome->collision_type = kRectCollisionType;
cpCCSprite *dome1 = [cpCCSprite spriteWithShape:dome file:@"001.png"];
[self addChild:dome1];
dome1.spaceManager = smgr;
cpShape *pillarone = [smgr addRectAt:cpv(300,300) mass:1 width:45 height:194 rotation:0];
pillarone->collision_type = kRectCollisionType;
cpCCSprite *pillar1 = [cpCCSprite spriteWithShape:pillarone file:@"004.png"];
[self addChild:pillar1];
pillar1.spaceManager = smgr;
cpShape *pillartwo = [smgr addRectAt:cpv(500,300) mass:1 width:45 height:194 rotation:0];
pillartwo->collision_type = kRectCollisionType;
cpCCSprite *pillar2 = [cpCCSprite spriteWithShape:pillartwo file:@"004.png"];
[self addChild:pillar2];
pillar2.spaceManager = smgr;
cpShape *staticground = [smgr addRectAt:cpv(510,25) mass:1 width:0 height:0 rotation:0];
cpCCSprite *staticground1 = [cpCCSprite spriteWithShape:staticground file:@"grass1-1024.png"];
[self addChild:staticground1 z:1 tag:0];
[smgr addCollisionCallbackBetweenType:kRectCollisionType
otherType:kBallCollisionType
target:self
selector:@selector(handleCollisionWithFragmentingRect:arbiter:space:)];
此处是碰撞处理的代码。
- (void) handleCollisionWithFragmentingRect:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space{
if (moment == COLLISION_POSTSOLVE)
{
[self removeChild:ball->data cleanup:YES];
[smgr removeAndFreeShape:ball];
ball = [smgr addCircleAt:cpv(1000,10) mass:0.5 radius:35];
ball->collision_type = kBallCollisionType;
ballSprite = [cpCCSprite spriteWithShape:ball file:@"head.png"];
[self addChild:ballSprite];
ballSprite.spaceManager = smgr;
}
}
当第一个球击中支柱时,它会消失。但是对于第二次和有时候第三次接球并且撞到了柱子,它会因为错误而崩溃,如下所示。
Chipmunk warning: Cannot remove a constraint that was not added to the space. (Removed twice maybe?)
Failed condition: cpArrayContains(space->constraints, constraint)
我不知道哪里出错了,请有人帮忙。
感谢。
答案 0 :(得分:0)
我认为你需要添加要移除到“垃圾”数组的球,而不是在handleCollisionWithFragmentingRect函数上删除它。对于所有安全措施,包括
- (void) update : (ccTime) dt
{
for (cpShape *shape in _garbage)
{
// Remove shape
[self removeChild:shape->data cleanup:YES];
[smgr removeAndFreeShape:shape];
// Add new ball
ball = [smgr addCircleAt:cpv(1000,10) mass:0.5 radius:35];
ball->collision_type = kBallCollisionType;
ballSprite = [cpCCSprite spriteWithShape:ball file:@"head.png"];
[self addChild:ballSprite];
ballSprite.spaceManager = smgr;
}
...
}
然后你将把句柄功能更改为:
- (void) handleCollisionWithFragmentingRect:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
if (moment == COLLISION_POSTSOLVE)
{
// SHOULDNT BE A DIRECT REFERENCE TO MEMBER VARIABLE BALL.
// SHOULD BE ADDED FROM INFORMATION WITHIN THE PARAMETERS OF THIS FUNCTION.
_garbage.addObject(ball);
}
}