0
votes

I'm developing a game with cocos2d and box2d. The problem is that sometimes my app crashes when I touch the screen to make the player jump.

Here is how I implemented jump action in my Player class:

- (void)jump
{
if (numFootContacts > 0)
    {
        isJumping = YES;
        b2Vec2 impulse = b2Vec2(0.0f, 100.0f);
        b2Vec2 point = body->GetWorldCenter();      // this line sometimes triggers an EXC_BAD_ACCESS for the body pointer.

        body->ApplyLinearImpulse(impulse, point);
    }
}

numFootContacts allows me to know if I'm on the ground or not.

And in my game scene I have this (and other non relevant stuff):

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    [player jump];
    return YES;
}

From what I have seen in some other people codes, this is a common way to implement a jump action.

I think the problem comes from the physics world update method.
Because I implemented the jump action in another way, where I only jump ([player jump]) after the world->Step method, and I don't get any EXC_BAD_ACCESS.

So does anyone already has this kind of problem?
I think it's very similar to the "problem" that you can't remove a body during callbacks because the body is locked. Here the problem is that maybe I jump just right when the world is updating. The bug is kind of hard to reproduce (1 time over 10).
Tell me if I'm wrong.

1

1 Answers

0
votes

If you are calling [player jump] from touch handling method and your app is not multithreaded then ccTouchBegan can't be called during Step function execution. The reason you are getting EXC_BAD_ACCESS is that you are accessin invalid pointer somewhere. It can be player, body or anything else.