2012-12-27 37 views
0

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

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

回答

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。