Cocos2D 中的碰撞检测
我正在尝试检测 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题描述表明精灵从未从内存中删除。如果您使用 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.