0

让我深入解释一下,当if(CGRectContainsPoint([hole1 boundingBox], ball1.position))条件成立时,我会做很多事情,比如计划外、选择器、破坏球体调用动画(请参阅下面的代码)等。这在大多数情况下都能正常工作。但有时当球真的接近球洞时(只是接触球洞但不足以使上述条件成立),或者被以非常快的速度抛向球洞,则应用程序被终止。我已经通过评论在本节中执行的许多操作进行了检查,但没有任何帮助,当采取一些措施使其终止时,应用程序会继续终止。

if(CGRectContainsPoint([hole1 boundingBox], ball1.position))
{
    ballBody->SetLinearVelocity(b2Vec2(0.0f,0.0f));
    ballBody->SetAngularVelocity(0.0f);

    [self unschedule:@selector(tick:)];
    self.isTouchEnabled = NO;

    [self removeChild:ball1 cleanup:YES];
    world->DestroyBody(ballBody);

    // create the sprite sheet
    CCSpriteSheet *spriteSheet;

    GolfBallsAppDelegate *appDelegate = (GolfBallsAppDelegate *)[[UIApplication sharedApplication] delegate];
    if([appDelegate.ballValue isEqualToString:@"cricketball"])
    {
        spriteSheet = [CCSpriteSheet spriteSheetWithFile:@"cricket_ball_strip.png"];
    }
    else if([appDelegate.ballValue isEqualToString:@"ironball"])
    {
        spriteSheet = [CCSpriteSheet spriteSheetWithFile:@"iron_ball_strip.png"];
    }
    else if([appDelegate.ballValue isEqualToString:@"golfball"])
    {
        spriteSheet = [CCSpriteSheet spriteSheetWithFile:@"golf_ball_strip.png"];
    }
    else if([appDelegate.ballValue isEqualToString:@"soccerball"])
    {
        spriteSheet = [CCSpriteSheet spriteSheetWithFile:@"soccer_ball_strip.png"];
    }
    else if([appDelegate.ballValue isEqualToString:@"basketball"])
    {
        spriteSheet = [CCSpriteSheet spriteSheetWithFile:@"basket_ball_strip.png"];
    }

    spriteSheet.position = ccp(hole1.position.x,60);
    [self addChild:spriteSheet];

    float frameWidth = 96;
    float frameHeight = 84;

    CCSprite *sprite = [CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(0, 0, frameWidth, frameHeight)];

    [spriteSheet addChild:sprite];

    //if(animation)
    {
        // create the animation
        CCAnimation *spriteAnimation = [CCAnimation animationWithName:@"potting" delay:0.1f];

        int frameCount = 0;
        for (int x = 0; x < 6; x++) 
        {
            // create an animation frame
            CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:spriteSheet.texture rect:CGRectMake(x*frameWidth,0*frameHeight,frameWidth,frameHeight) offset:ccp(0,0)];
            [spriteAnimation addFrame:frame];

            frameCount++;

            // stop looping after we've added 14 frames
            if (frameCount == 6)
            {
                //[self removeChild:spriteSheet cleanup:YES];
                break;
            }
        }

        // create the action
        CCAnimate *spriteAction = [CCAnimate actionWithAnimation:spriteAnimation];
        //CCRepeatForever *repeat = [CCRepeatForever actionWithAction:spriteAction];

        // run the action
        [sprite runAction:spriteAction];
        //[sprite runAction:repeat];
    }
    [self schedule:@selector(loading) interval:0.5];
    [self schedule:@selector(holeFinish) interval:1];
    //[self removeChild:spriteSheet cleanup:YES];
}

任何建议将不胜感激。

编辑:我发现的是,以下行有问题[self removeChild:ball1 cleanup:YES]; world->DestroyBody(ballBody); (可能是)。但由于它并不总是发生,(正如我所提到的),因此它很荒谬。

4

1 回答 1

2

我认为您的问题将是当 b2World 被“锁定”时(当世界忙于解决碰撞时)您试图删除一个身体。

尝试将对象标记为准备删除,并在下一个循环开始时将其删除:

代替:

[self removeChild:ball1 cleanup:YES];
world->DestroyBody(ballBody);

ball1.isDead = YES;

在下一个游戏循环开始时:

for (Ball b in balls)
{
    if (b.isDead)
        world->DestroyBody(b.ballBody);
}
于 2011-08-31T07:31:03.490 回答