0

我需要帮助来销毁碰撞精灵内部和周围的精灵,即在 2.5 厘米的半径内,所有精灵都应该被破坏。这里的想法是我将从底部向从顶部掉落的物体射击射弹。一旦发生碰撞,该半径周围的所有精灵也应该被销毁。像炸弹效应。我已经使用 box2d 进行碰撞,即联系侦听器。该怎么做呢?

请建议:-)

问候,

卡尔提克

4

1 回答 1

1

保存一个精灵数组,或者如果您使用的是 batchNode,您可以这样做。

当碰撞发生时,通过你的精灵。检查距离与他们的位置和爆炸中心,如果他们在范围内,则杀死他们。

例如

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


}

希望有帮助。

于 2010-12-14T11:16:40.770 回答