cocos2d check collision
I want to check collision of birds and fishes.
I have a BirdCache
which contains
-(void) isB开发者_开发百科irdCollidingWithRect:(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?
So this work like that:
- for every living bird you check if it collides with any fish, and if it does, you kill the bird
- for every living fish you check if it collides with any bird, and if it 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.
精彩评论