开发者

How to destroy a body based on its position in box2d?

I am new to objective-c. I am currently working on a game using cocos2d and box2d. Let me describe my game in details:

In my game I have a main character standing on top of a building. Below the building there's this the road. Enemies pass by the road at various random speeds entering the screen from right and exiting from the left. I have created the enemies as b2_dynamicBodies and set a random velocity for each of them using SetLinearVelocity(). The main character shoots the enemies. When the projectile hits the enemies, both the projectile and the enemy are destroyed. What I want to do is that the enemies which are not shot and which move out of the screen, I want to destroy these enemies once they move out of the screen by cheking their coord开发者_StackOverflowinates. I have tried using world->DestroyBody(body), but it doesnt work. Pls help me with this!

p.s. I am not creating the enemies as individual distinct bodies, but I have a method which creates a body and makes it move and then I have called the method inside init with a time interval.


Assign tag to the sprite.Here i am using 10.check that sprite moves out off the screen by getting their x-position.If it is Less than 0 or greater than 480(for landscape).Then use the following code to destroy that body.

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{

    if (b->GetUserData() != NULL) {

        CCSprite *temp1 = (CCSprite*)b->GetUserData();

        if(temp1.tag == 10)
        {
            NSLog(@"Destroying here");
                            world->DestroyBody(b);
            [self removeChild:temp1 cleanup:YES];


        }

    }

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜