开发者

Cocos2d game termination problem when a moving sprite goes inside the bounding box of a animating still sprite(a ball get into a hole)

开发者 https://www.devze.com 2023-03-31 17:12 出处:网络
Let me explain it indepth, when ever if(CGRectContainsPoint([hole1 boundingBox], ball1.position)) condition goes true, i do lots of stuffs, like unscheduled, a selector, destroying a ball body calling

Let me explain it in depth, when ever if(CGRectContainsPoint([hole1 boundingBox], ball1.position)) condition goes true, i do lots of stuffs, like unscheduled, a selector, destroying a ball body calling an animation (please refer Code below)etc. This work properly most of the time. But sometimes when ball is really near to hole(just touching the hole but but not enough to make the above condition true), or is been throws towards the hole really fast speed, then application got terminated. I have checked, by commenting many actions which are been performed in this section, but got nothing helpful, application keep terminating when some efforts are been done to make it terminate.

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:@"crick开发者_如何学Cetball"])
    {
        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];
}

Any suggestion will be highly appreciated.

EDIT: What i found is, problem with folling lines [self removeChild:ball1 cleanup:YES]; world->DestroyBody(ballBody); (MAY be). but as it not occurs always, (as I mentioned), thus it's being ridiculous.


I think your problem will be that you are trying to delete a body when the b2World is 'locked', (When the world is busy working out collisions).

Try flagging the object as ready for deletion, and deleting it at the start of your next loop:

Replace:

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

with

ball1.isDead = YES;

And at the start of your next game loop:

for (Ball b in balls)
{
    if (b.isDead)
        world->DestroyBody(b.ballBody);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号