2011-11-17 83 views
0

因为我havnt在我的previos后得到了答案,并没有人解决这个问题,我再次问这个。对此没有任何解释。一个特定的精灵触摸检测

我需要检测一个精灵触摸,一个具体的。在cocos2d + box2d中。

让我说我有精灵CCSprite *ran有一个身体,但是,我有很多它。

,如果我检测与ccTouchesBegan触摸,并使用if(CGRectContainsPoint(particularSpriteRect, currentPosition))

我将检测到触摸的ran但我不知道谁跑是这样的一切,我需要摧毁不是具体ran,我不知道是谁。

我觉得做的最好的方式,因为我接触监听器,让我具体精灵的用户数据做:

CCSprite *actora = (CCSprite*)bodyA->GetUserData(); 
CCSprite *actorb = (CCSprite*)bodyB->GetUserData(); 

,然后我知道actora是什么需要被破坏的原因,我有他的用户数据。

[self removeChild:actora cleanup:YES]; 

所以,再次,我需要检测精灵的触摸和知道是谁,因为我有很多ran's。 我想它的东西需要涉及userData。

请问有什么方向吗? 非常感谢。

回答

0

好吧,我得到它:

这是我做什么,摧毁特定的身体,从不同的东西在这里说:

把下面的代码在触摸法:

CGPoint currentPosition = [touch locationInView: [touch view]]; 
currentPosition = [[CCDirector sharedDirector] convertToGL: currentPosition]; 

    b2Vec2 locationWorld = b2Vec2(currentPosition.x/PTM_RATIO, currentPosition.y/PTM_RATIO); 

    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) 
    { 
     b2Fixture *bf1 = b->GetFixtureList(); 
     if (bf1->TestPoint(locationWorld)) 
     { 
      CCSprite *tempSprite = (CCSprite *) b->GetUserData(); 
      if (tempSprite .tag==2 || tempSprite .tag==3) 
      { 
       [self removeChild:tempSprite cleanup:YES]; 
       world->DestroyBody(b); 

      } 
     } 
    } 

在这里,如果你触摸右精灵(标记),它会被破坏。