Cocos2D 中的碰撞检测

发布于 2024-10-16 04:18:58 字数 1115 浏览 3 评论 0原文

我正在尝试检测 cocos2D 中的碰撞。我正在使用这段代码:

- (void)checkForCollisionSpeedUp:(ccTime)dt

{

    CGRect projectileRect = CGRectMake(
                                       guy.position.x, 
                                       guy.position.y, 
                                       2, 
                                       20);


        CGRect targetRect = CGRectMake(
                                       speedUp.position.x - (speedUp.contentSize.width/2), 
                                       speedUp.position.y - (speedUp.contentSize.height/2), 
                                       speedUp.contentSize.width, 
                                       speedUp.contentSize.height);
        if (CGRectIntersectsRect(projectileRect, targetRect)) {
            [[SimpleAudioEngine sharedEngine] playEffect:@"Robot_blip-Marianne_Gagnon-120342607.wav"];
            [bg removeChild:speedUp cleanup:YES];

    }
}

该代码检测到碰撞,播放声音,并删除精灵,但不删除 CGRect。 CGRect 保留在精灵被删除时的位置。我该如何解决这个问题?

谢谢,

Tate

另外,我真的不想使用 Box2D 或 Chipmunk 进行碰撞检测。

I am trying to detect collisions in cocos2D. I am using this code:

- (void)checkForCollisionSpeedUp:(ccTime)dt

{

    CGRect projectileRect = CGRectMake(
                                       guy.position.x, 
                                       guy.position.y, 
                                       2, 
                                       20);


        CGRect targetRect = CGRectMake(
                                       speedUp.position.x - (speedUp.contentSize.width/2), 
                                       speedUp.position.y - (speedUp.contentSize.height/2), 
                                       speedUp.contentSize.width, 
                                       speedUp.contentSize.height);
        if (CGRectIntersectsRect(projectileRect, targetRect)) {
            [[SimpleAudioEngine sharedEngine] playEffect:@"Robot_blip-Marianne_Gagnon-120342607.wav"];
            [bg removeChild:speedUp cleanup:YES];

    }
}

That code detects the collision, it plays the sound, and removes the sprite, but not the CGRect. The CGRect remains in the position of the sprite when it was deleted. How do I solve this?

Thanks,

Tate

Also, I really don't want to use Box2D or Chipmunk for collision detection.

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

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

发布评论

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

评论(1

流云如水 2024-10-23 04:18:58

您的问题描述表明精灵从未从内存中删除。如果您使用 alloc/init 创建它,您可能忘记释放它。如果您从自动释放初始化程序创建它,您可能会保留它。

另外,使用 [selfboundingBox] 方法来获取精灵的边界框。它更快、更灵活。

Your problem description indicates that the sprite was never removed from memory. If you created it with alloc/init you probably forgot to release it. If you created it from autorelease initializer, you probably retained it.

Also, use the [self boundingBox] method to get a sprite's bounding box. It's faster and more flexible.

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