Create body on a sprite (box2D)

| | August 5, 2015

I’m quite experimented in objective C, but now i’m tried cocos2D and his features.

I need to add a body on a spritesheet (if possible on a classic ‘UIImageView’), in order to detect collisions after.

Is there a way to create a box2D body, and that it suits sprite position ?

Because i wrote :

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    CGPoint current = myUIImageView.center;
    int accelerator = 25;
    if (current.y > 70 && current.y < 407)
    {
        current.y -= acceleration.y*accelerator;
    }
    else if (current.y <= 70)
    {
        if (acceleration.y < 0)
        {
            current.y -= acceleration.y*accelerator;
        }
    }
    else if (current.y >= 407)
    {
        if (acceleration.y > 0)
        {
            current.y -= acceleration.y*accelerator;
        }
    }
    myUIImageView.center = current;
}

and found some codes, but looks very hard…

Thanks for your help !

One Response to “Create body on a sprite (box2D)”

  1. Have you looked into CCPhysicsSprite?

    Otherwise you can use the b2body.userData pointer to store sprite information after you’ve created your physics body.

Leave a Reply