将身体附加到精灵

时间:2012-12-27 21:50:43

标签: cocos2d-iphone touch box2d sprite

我正在尝试使用Cocos2d 2.0和box2d测试/创建示例游戏。我在屏幕上有一堆精灵,当我按下Sprite时,我想要一个身体自动附加到该Sprite上。我试图使用TouchesEnd方法,但它似乎不起作用。

有人能把我推向正确的方向吗?

1 个答案:

答案 0 :(得分:0)

试试这种方式......

-(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);

}

只有触摸?然后使用ccTouchesBegan。