I am really trying to get my head round collision detection, here are my game details: I have a character that can move freely around the screen and fire bullets Enemies are generated off screen and the ‘hero’ can shoot them (when bullets and enemies are created they are stored in arrays and then removed at collision) Currently if the enemy makes contact with the hero then nothing happens, I would like a life to be removed on contact and believe this is done via collision detection. I am using
(CGRectIntersectsRect([hero boundingBox], [enemy boundingBox]))
But not all collisions are detected and then suddenly 3 will detected. I believe that this is caused as multiple collisions are detected as the objects pass through each other. I have tried to use a BOOL flag to but I don’t believe that I am doing it correctly, my code:
.h
BOOL collision;
.m
-(void)update:(ccTime)deltaTime {
if (collision == NO) {
if (CGRectIntersectsRect([hero boundingBox], [enemy boundingBox])) {
CCLOG(@”collision detected!!!!!!!!!!!!!!!!”);
collision = YES;
}
}
}
Is this the best way to deal with collision detection and if so how do you implement the BOOL flag?