我正在寻找在第一次触摸时在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 :(得分: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;
}
}
}
}
我希望这会对你有所帮助。