2011-11-17 47 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); 

      } 
     } 
    } 

在這裏,如果你觸摸右精靈(標記),它會被破壞。