cocos2d 检查碰撞
我想检查鸟类和鱼类的碰撞。
我有一个BirdCache
,其中包含
-(void) isBirdCollidingWithRect:(CGRect) rect
检查缓存中的每只可见鸟
-(void) update:(ccTime) delta
,它调用isFishCollidingWithRect
来测试每只鸟,如果YES
,bird .visible = NO
和一个 FishCache
(类似于 BirdCache
),
但结果是鱼在碰撞时永远不会死。 (我确实在两个更新中都设置了visible = NO,
我认为问题是首先当bird.visible = NO
时的竞赛条件,然后鱼不会与鸟碰撞。然后我尝试 schedule:selector(delayedInvisible) Interval: 1.0 / 10.0f
,但仍然失败。
是否有解决此类问题的通用方法?
I want to check collision of birds and fishes.
I have a BirdCache
which contains
-(void) isBirdCollidingWithRect:(CGRect) rect
which check every visible bird in the cache
-(void) update:(ccTime) delta
which call isFishCollidingWithRect
to test every bird, if YES
, bird.visible = NO
and a FishCache
(similar to BirdCache
)
but the result is the fish never die when they collide. (I do set visible = NO
in both update
I think the problem is racing condition when bird.visible = NO
first, then fish will not be colliding with the bird. then I tried to schedule:selector(delayedInvisible) interval: 1.0 / 10.0f
, but still failed.
Is there any common approach to this kind of problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以这个工作是这样的:
与任何鱼发生碰撞,如果它
是的,你杀死
所以,
如果鸟与鱼相撞,只有鸟会被杀死。
您必须在每种方法中杀死两个碰撞对象,或者向鸟类和鱼类添加字段(该动物应该在下一帧被杀死),并将其设置为碰撞,而不是直接设置可见字段。
So this work like that:
collides with any fish, and if it
does, you kill the bird
does, you kill the fish
So if bird collides with fish only bird is killed.
You have to kill both colliding objects in each method, or add field to birds and fishes that says (this animal should be killed next frame), and set this on collision, not the visible field directly.