感謝您的時間!如何在延遲時間後刪除sprite/b2body?
我正在用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」會指向第二隻鳥,那麼我該如何刪除第一隻鳥呢?
謝謝!
感謝您的回覆!但是對於「actionWithTarget:bird」,現在這隻鳥是指新鳥,而不是第一隻。我想知道如何刪除第一個。我想我應該使用一個NSMutableArray,但我不太確定如何做到這一點。 – alanlzl
檢查我的編輯,我希望它很明確 – wingNut86
謝謝!這有幫助! – alanlzl