微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当删除spacemanager-cocos2d中的形状时,程序崩溃

如何解决当删除spacemanager-cocos2d中的形状时,程序崩溃

|| 我在我的游戏中使用带有花栗鼠物理学的spacemanager。 我想做的是将球击中支柱并使球消失。我在不同的位置重新创建球,以便用户可以击中另一个支柱。我将球的形状和球的精灵作为成员变量。 这是用于创建球,柱形等的代码
   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)
我不确定我哪里出错了,任何人都可以帮忙。 谢谢。     

解决方法

        我认为您需要将要删除的球添加到“垃圾”数组中,而不要在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;
    }
    ...

}
然后您将handle函数更改为:
- (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);
    }
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。