Reddevil
Reddevil

Reputation: 1

Destroying Sprites in and around the Collided Sprite

I need help in destroying the sprites which are in and around the collided sprites ie in a radius of 2.5 cms all sprites should be destroyed. Idea here is i will be shooting a projectile from bottom to the objects falling from the top. Once collision happens all the sprites around that radius also should be destroyed. Like a Bomb Effect. I have used box2d for collision ie contact listener. How to go about doing that?

Please Suggest:-)

Regards,

Karthik

Upvotes: 0

Views: 206

Answers (1)

Bongeh
Bongeh

Reputation: 2300

Hold an array of your sprites, or if you are using a batchNode you can do that.

When the collision happens, go through your sprites. Check the distance with their position and the explosion center and kill them if they are in range.

e.g.

CCSprite *sprite;
for (sprite in [batchNode descendants]) {

   if ([sprite isInRangeOf:[explosionSprite position]]) {
       [sprite yourRemovalMethod];
   }

}

the method 'isInRangeOf:' would be within your sprite subclass

Something like..

-(BOOL) isInRangeOf:(CGPoint)explosionCenter {

 //Use pythagoras theorem to work out the distance between [sprite position] and [explosionCenter]

    CGFloat dx = explosionCenter.x - [self position].x;
    CGFloat dy = explosionCenter.y - [self position].y;
    float distance = sqrt(dx*dx + dy*dy );

 // If your distance is less than or equal to your 'death radius' return YES, else No.
    if (distance <= 25) {
    return TRUE;
    } else { 
    return FALSE;
    }


}

Hope that helps.

Upvotes: 1

Related Questions