Question

In my code i've always used spriteA = (__bridge CCSprite *) bodyA->GetUserData(); //where spriteA is a CCSprite and bodyA is a b2Body. I use it to get whatever sprite is linked to bodyA. My problem is, how do I do this the other way around? I have a sprite and I want to find out what b2Body is linked to it. How do I do this?

Edit

I don't know wether I set it up right or not, but I'm trying to remove all b2bodies (and sprites) in an array called row4 once there are no more blue objects (objects in row4BlueArray) Here is part of the code in my tick method:

//Find the sprite for the b2Bodies
 else if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
        spriteA = (__bridge CCSprite *) bodyA->GetUserData();
        spriteB = (__bridge CCSprite *) bodyB->GetUserData();
        contactPositionX = spriteA.position.x;
        contactPositionY = spriteB.position.y;
        //If sprite is a member of row 4 (tag 400)
        if (spriteA.tag == 400 && spriteB.tag == 8)
        {
            [self createExplosionBlue];
            [self addTileScore];
            [self removeChild:spriteA cleanup:YES];
            [self removeChild:spriteB cleanup:YES];
            NSLog(@"row 4 count: %d",row4BlueArray.count);
            //Remove object from another array
            [row4BlueArray removeLastObject];
            toDestroy.insert(bodyA);
            toDestroy.insert(bodyB);
            [self unschedule:@selector(tick:)];
            ballCount = 0;
            //if that array is empty, then remove all objects from this array (row4)
            if (row4BlueArray.count == 0) {
                for (b2Body * b = _world->GetBodyList(); b != NULL; b = b->GetNext()) {
                    Box2DSprite * sprite = (__bridge Box2DSprite*) b->GetUserData();
                    b2Body * spriteBody = sprite.body;
                    //not sure how to remove all bodies in an array (row4)`
                }

            }
        }
Was it helpful?

Solution

One simple way to do this is to cache the body as a property of a class.

You could extend the CCSprite class with a class called Box2DSprite. In Box2DSprite, you include a property for the b2Body * you used to create the physics for your sprite. You can also store a reference to the world if you think you may need it.

@interface Box2DSprite : CCSprite {


b2Body * body;
b2World * world;

} // end ivars

@property (readwrite) b2Body * body;
@property (readwrite) b2World * world;

Also remember to rename your .m file to a .mm file

Edit

In your game loop, to obtain the Box2DSprite you simply need to cast it back to a Box2DSprite.

for (b2Body * b = world->GetBodyList(); b != NULL; b = b->GetNext()) {

    Box2DSprite * sprite = (__bridge Box2DSprite*) b->GetUserData();
    b2Body * spriteBody = sprite.body;
    // Do stuff with body here
}

You just need to ensure that whenever you create a sprite with a physics body, you are creating a Box2DSprite.

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top