感谢你的时间!
我正在用box2d做一个项目。当我第一次触摸屏幕时,会在box2d世界中创建一只鸟,如下所示
- (void)createBird
{
//init bird
bird = [CCSprite spriteWithFile:@"bird.png"];
bird.scale = 23/[bird boundingBox].size.width;
bird.position = center;
[self addChild:bird z:12];
}
然后,如果触摸结束,那么鸟就会受力
//add box2d body
b2BodyDef birdBodyDef;
birdBodyDef.type=b2_dynamicBody;
birdBodyDef.userData = (__bridge void*)bird;
birdBodyDef.position.Set(bird.position.x/PTM_RATIO,bird.position.y/PTM_RATIO);
birdBody=world->CreateBody(&birdBodyDef);
//create circle shape
b2CircleShape birdShape;
birdShape.m_radius=([bird boundingBox].size.width-6)/PTM_RATIO/2;
// Create shape definitio and add to body
b2FixtureDef birdFixtureDef;
birdFixtureDef.shape=&birdShape;
birdFixtureDef.density=1.6f;
birdFixtureDef.friction=0.3f;
birdFixtureDef.restitution=0.3f;
birdFixture = birdBody -> CreateFixture(&birdFixtureDef);
// Apply force
b2Vec2 force = b2Vec2(-2.0f*distance,-2.0f*distance);
birdBody->ApplyForce(force,birdBody->GetWorldCenter());
birdBody->SetLinearDamping(0.2f);
然后,当我再次触摸屏幕时,将使用“createBird”方法创建另一只鸟。
我想在创建5秒后删除第一只鸟。但那时,“CCSprite * bird”和“b2Body * birdBody”将指向第二只鸟,那么如何删除第一只鸟呢?
谢谢!
答案 0 :(得分:0)
我不确定我完全理解你的问题,但这可能有所帮助。在创建新鸟之前打电话。
if ( bird ) {
id delayTimeAction = [CCDelayTime actionWithDuration:5.0];
id removeMySprite = [CCCallFuncND actionWithTarget:bird selector:@selector(removeFromParentAndCleanup:) data:(void*)NO];
[bird runAction:[CCSequence actions: delayTimeAction, removeMySprite, nil]];
}
编辑:
初始化数组
NSMutableArray *birds = [NSMutableArray new];
然后在创建鸟类功能中调用它
[birds addObject:newBird];
if ( [birds count] > 1 ) {
oldBird = [birds firstObject];
//Call remove function on oldBird
[birds removeObject:oldBird];
}
答案 1 :(得分:0)
一种方法是给你的鸟牌,然后将它们保存到Nsmutuable词典中。 继续遍历那个字典,然后在你的bool变量设置为destroy之后摧毁身体......
我使用类做了同样的事情....事实上,无论如何你都需要摧毁身体。 (否则你将在以后遇到fps问题)