我正在寻找方法来创建一个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倍,再一次摧毁它。我需要做的是让你点击一次来创建身体,然后再次点击摧毁?
你还没有告诉我们这个代码有什么问题。你在哪里遇到问题? – LearnCocos2D