摧毁碰撞精灵内部和周围的精灵

发布于 2024-10-07 08:14:48 字数 181 浏览 3 评论 0原文

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

请建议:-)

问候,

Karthik

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春夜浅 2024-10-14 08:14:48

保存你的精灵数组,或者如果你使用的是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;
    }


}

希望有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文