2011-10-21 55 views
0

我正在寻找方法来创建一个Box2D的身体与第一次触摸 并第二次触摸摧毁它。触摸创建身体触摸破坏身体

要创建我使用这种方法体:

(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView: [touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL: location]; 

    if (touch.tapCount > 1) { 
     CGSize winSize = [CCDirector sharedDirector].winSize; 
     CCSprite *sprite = [CCSprite spriteWithFile:@"sprite.png" rect:CGRectMake(0, 0, 90, 90)]; 

     sprite.position = ccp(location.x/PTM_RATIO, location.y/PTM_RATIO); 
     sprite.tag = 7; 
     [self addChild: sprite]; 

     b2BodyDef ballBodyDef2; 
     ballBodyDef2.type = b2_dynamicBody; 
     ballBodyDef2.position.Set(location.x/PTM_RATIO, location.y/PTM_RATIO); 
     ballBodyDef2.userData = sprite; 

     b2Body *body2 = _world->CreateBody(&ballBodyDef2); 

     b2CircleShape circle; 
     circle.m_radius = 22.0/PTM_RATIO;//(arc4random()*26.0)/PTM_RATIO; 

     b2FixtureDef ballShapeDef2; 
     ballShapeDef2.shape = &circle; 
     ballShapeDef2.density = 1.0f; 
     ballShapeDef2.friction = 0.5f; 
     ballShapeDef2.restitution = 0.2f; 

     body2->CreateFixture(&ballShapeDef2); 
    } else { 
     b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.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==7) { 
        [self removeChild:tempSprite cleanup:YES]; 
        _world->DestroyBody(b); 
       } 
      } 
     } 
    } 
} 

通过这种方法,创造了身体,我有触摸屏的2倍,再一次摧毁它。我需要做的是让你点击一次来创建身体,然后再次点击摧毁?

+0

你还没有告诉我们这个代码有什么问题。你在哪里遇到问题? – LearnCocos2D

回答

0

通过这种方式创建的身体当轻敲一次,然后再次点击摧毁

if (isBodyCreated ==NO) 
    { 
     isBodyCreated=YES; 
     CGSize winSize = [CCDirector sharedDirector].winSize; 
     CCSprite *sprite = [CCSprite spriteWithFile:@"Icon.png"]; 

     sprite.position = ccp(location.x/PTM_RATIO, location.y/PTM_RATIO); 
     sprite.tag = 7; 
     [self addChild: sprite]; 

     b2BodyDef ballBodyDef2; 
     ballBodyDef2.type = b2_dynamicBody; 
     ballBodyDef2.position.Set(location.x/PTM_RATIO, location.y/PTM_RATIO); 
     ballBodyDef2.userData = sprite; 

     b2Body *body2 = world->CreateBody(&ballBodyDef2); 

     b2CircleShape circle; 
     circle.m_radius = 22.0/PTM_RATIO;//(arc4random()*26.0)/PTM_RATIO; 

     b2FixtureDef ballShapeDef2; 
     ballShapeDef2.shape = &circle; 
     ballShapeDef2.density = 1.0f; 
     ballShapeDef2.friction = 0.5f; 
     ballShapeDef2.restitution = 0.2f; 

     body2->CreateFixture(&ballShapeDef2); 
    } 
    else 
    { 
     b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.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==7) 
       { 
        [self removeChild:tempSprite cleanup:YES]; 
        world->DestroyBody(b); 
        isBodyCreated=NO; 
       } 
      } 
     } 
    } 

我希望这会帮助你。

+0

是完美的。 1000谢谢 – user1007760

+0

奇怪。 由巴努建议的代码是完美的,但我不明白为什么,模拟器是好的,但在iPhone上崩溃 – user1007760

+0

请等待我会检查 – banu