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?
So this work like that:
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.