在Collided Sprite中及其周围销毁Sprite

时间:2010-12-14 06:00:01

标签: iphone cocos2d-iphone box2d box2d-iphone

我需要帮助来破坏碰撞精灵内部和周围的精灵,即半径为2.5厘米的所有精灵都应该被摧毁。这里的想法是我将从底部拍摄射弹到从顶部落下的物体。一旦发生碰撞,该半径周围的所有精灵也应该被摧毁。就像炸弹效应一样。我使用box2d进行碰撞,即联系监听器。怎么去那样做?

请建议: - )

此致

KARTHIK

1 个答案:

答案 0 :(得分:1)

保留一组精灵,或者如果你使用的是batchNode,你可以这样做。

碰撞发生时,请仔细检查。检查它们的位置和爆炸中心的距离,如果它们在范围内,则将它们杀死。

e.g。

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

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

}

方法'isInRangeOf:'将在你的精灵子类

像...这样的东西。

-(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;
    }


}

希望有所帮助。