2012-08-17 112 views
1

我正试图移动触摸移动精灵。但是当两个精灵在那里时,我正在接触两个精灵。这就是为什么精灵不能正常移动。cocos2D和Box2D:如何获得精确触摸的精灵?

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
{ 

if (_mouseJoint != NULL) return; 

    UITouch *myTouch = [touches anyObject]; 
    CGPoint location = [myTouch locationInView:[myTouch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 
    b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); 

    for(int i=0;i<[mutArrFixtures count];i++) 
    { 
     b2Fixture *fixture; 
     [[mutArrFixtures objectAtIndex:i] getValue:&fixture]; 

     if (fixture->TestPoint(locationWorld)){ 

      for(int j=0; j<[mutArrPaddleBody count]; j++) 
      { 
       b2Body *body; 
       [[mutArrPaddleBody objectAtIndex:j] getValue:&body]; 
       b2MouseJointDef md; 
       if(body == fixture->GetBody()) 
       { 
        md.bodyA = _groundBody; 
        md.bodyB = body; 
        md.target = locationWorld; 
        md.collideConnected = true; 
        md.maxForce = 1000.0f * body->GetMass(); 

        _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md); 
        body->SetAwake(true); 
       } 
      } 
     } 
    } 
} 

我有b2Fixture mutArrFixtures的阵列,并且还b2body阵列mutArrPaddleBody。但在这个如果我触摸第二个精灵我碰到第一个精灵和第二个精灵..两个精灵位置是相同的...

回答

1

在触摸功能,检查标签的精灵。如果那是你的精灵然后移动。给我看一些你用于触摸移动的代码。

.......

与下一个

  b2Body *body; 
      [[mutArrPaddleBody objectAtIndex:j] getValue:&body]; 
      b2MouseJointDef md; 
      if(body == fixture->GetBody()) 

替换这些代码

b2Body* body = fixture->GetBody(); 

CCSprite *sprite = (CCSprite*)body->GetUserData(); 

if(sprite && sprite.tag == kTagHero) 
{ 

} 

确保ü添加标签kTagHero乌拉圭回合移动精灵。

....

enum gameTag { 
    kTagHero = 1001 
}; 

并分配sprite.tag = kTagHero ......

+0

我在我的代码加入我的一些代码 – Hiren 2012-08-17 08:25:50

+0

'如果(fixture->测试点(locationWorld))'这个情况对于两个精灵 – Hiren 2012-08-17 08:46:10

+0

是真的我个人使用QueryCallback..indows代码风格..http://s463.codeinspot.com/q/1728579 – Guru 2012-08-17 08:51:59