2013-04-05 23 views

回答

-1

在短短用於與標籤去除精靈問題的代碼:5, 你需要check

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
//Getting Touch Locations 
UITouch *touch = [touches anyObject]; 
CGPoint location = [touch locationInView: [touch view]]; 
location = [[CCDirector sharedDirector] convertToGL: location]; //Converting to cocos2D positioning b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO); //Getting body list from world 
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) 
{ //Getting Fixture from the bodies 
    b2Fixture *bf1 = b->GetFixtureList(); //Checking whether the fixture contains touch location! 
    if (bf1->TestPoint(locationWorld)) 
    { //If YES assigning user data to a sprite tempSprite 
    CCSprite *tempSprite = (CCSprite *) b->GetUserData(); 
    //Checking whether the user data sprite tag is 5 
    if (tempSprite .tag==5) 
    { 
    //If YES then remove the sprite & the body! 
    [self removeChild:tempSprite cleanup:YES]; 
    world->DestroyBody(b); 
    } 
    } 
} 
} 
-1
//first get sprite by tag 
      b2Body *sprite = NULL; 
      //then give body to sprite 
      sprite = [(TRBox2D*)[self getChildByTag:5]body]; 

//then destroy by following method 
      _world->DestroyBody(sprite); 
      [self removeChildByTag:5 cleanup:YES];