如何用标记COCOS2D Box2D破坏精灵

时间:2013-04-05 06:59:05

标签: cocos2d-iphone box2d

CCSprite * sprite =(CCSprite *)[self getChildByTag:5];
 [self removeChildByTag:5 cleanup:YES];

2 个答案:

答案 0 :(得分:-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 :(得分:-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];