0
votes

I am trying to test/create a sample game using Cocos2d 2.0 and box2d. I have a bunch of sprites on the screen and when I press the Sprite, I want a body to be automatically attached to that Sprite. I tried to use the TouchesEnd method but it doesn't seem to work.

Can someone push me in the right direction?

1

1 Answers

0
votes

Try this way...

-(void)createB2Body
{
    b2PolygonShape shape;

    float xDist = (sprite.contentSize.width*0.5f)/PTM_RATIO ;
    float yDist = (sprite.contentSize.height*0.5f)/PTM_RATIO ;

    shape.SetAsBox(xDist, yDist);

    b2BodyDef bd;
    bd.type = b2_dynamicBody;
    bd.userData = sprite;
    bd.linearDamping = 0.5f;
    bd.angularDamping = 0.5f;

    bd.position.Set(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO);

    b2FixtureDef fixDef;
    fixDef.shape = &shape;
    fixDef.density = 1.0f;
    fixDef.friction = 0.1f;
    fixDef.restitution = 1.0f;
    fixDef.isSensor = true;

    self.body = self.world->CreateBody(&bd);

    self.body->CreateFixture(&fixDef);

}

Only on touch? then use ccTouchesBegan.