Cocos2d iphone 身体有两种不同的体质吗?

Cocos2d iphone 身体有两种不同的体质吗?,cocos2d-iphone,box2d,box2d-iphone,Cocos2d Iphone,Box2d,Box2d Iphone,我正在做一个box2d应用程序。我在世界上创造了两个不同的身体。检查此代码 -(void)init{ [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sheet.plist"]; CCSpriteBatchNode *parent = [CCSpriteBatchNode batchNodeWithFile:@"sheet.png" capacity:100]; spriteText

我正在做一个box2d应用程序。我在世界上创造了两个不同的身体。检查此代码

-(void)init{

 [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sheet.plist"];
    CCSpriteBatchNode *parent = [CCSpriteBatchNode batchNodeWithFile:@"sheet.png" capacity:100];
    spriteTexture_ = [parent texture];
    [self addChild:parent z:0 tag:kTagParentNode];
}
-(void)bodyCreation
{
 CCNode *parent = [self getChildByTag:kTagParentNode];

 PhysicsSprite *sprite = [PhysicsSprite spriteWithSpriteFrameName:@"bodySprite1.png"];
[parent addChild:sprite];

 b2BodyDef ballBodyDef;
 ballBodyDef.type = b2_dynamicBody;
 ballBodyDef.bullet = true;
 ballBodyDef.position.Set(1210.0f/PTM_RATIO, 250.0f/PTM_RATIO);
 randombody = world->CreateBody(&ballBodyDef);

 b2CircleShape circle;
 circle.m_radius = 16.0/PTM_RATIO;

 weightBallDef.shape = &circle;
 weightBallDef.density = 10.0f;
 weightBallDef.restitution = 0.2f;
 randomFixture = randombody->CreateFixture(&weightBallDef);
 [sprite setPhysicsBody:randombody];
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  CCNode *parent = [self getChildByTag:kTagParentNode];

  PhysicsSprite *sprite11 = [PhysicsSprite spriteWithSpriteFrameName:@"bodySprite2.jpg"];
  [parent addChild:sprite11];
  sprite11.position = ccp(touchBegin.x, touchBegin.y);

   b2BodyDef ballBodyDef;
   ballBodyDef.type = b2_dynamicBody;
   ballBodyDef.bullet = true;
   ballBodyDef.position.Set(touchBeginLocationWorld.x, touchBeginLocationWorld.y);
   ballbody = world->CreateBody(&ballBodyDef);

   b2PolygonShape box;
   box.SetAsBox(sprite11.contentSize.width/PTM_RATIO/2, sprite11.contentSize.height/PTM_RATIO/2);
   batShapeDef.shape = &box;
   batShapeDef.density = 10.0f;
   ballFixture = ballbody->CreateFixture(&batShapeDef);
   [sprite11 setPhysicsBody:ballbody];
}
我的预期结果如下:

但有时我会这样:

如何解决这个问题。谁能帮我


谢谢…

在处理之前将触摸转换为OpenGL(屏幕)坐标,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [[event allTouches] anyObject];
      CGPoint location = [touch locationInView: [touch view]];
      location = [CCDirector sharedDirector] convertToGL: location];
然后设置精灵的位置:

sprite11.position = location;
以及您的球体定义:

ballBodyDef.position.Set(location.x/PTM_RATIO, location.y/PTM_RATIO);

您是否尝试过只创建一个实体,并查看它是否正常?当点击屏幕时,一个实体工作正常CCtouchs开始创建实体后,我发现这个问题。