After continuously debugging and trying to fix this problem I am still have absolutely no luck…Can anyone please help me with this? It’s driving me insane..
I am using cocos2d v2 for iOS and basically I made a simple collision detection check method:
-(bool)collisionWithPoint:(CGPoint)center1 andRadius:(float)radius1 andPoint:(CGPoint)center2 andRadius:(float)radius2
{
float a = center2.x - center1.x;
float b = center2.y - center1.y;
float c = radius1 + radius2;
float distanceSquared = (a * a) + (b * b);
if (distanceSquared < (c * c)) {
return YES;
}
else {
return NO;
}
}
I also have an array keeping track of my enemies being added to the scene. I alloc in the init method:
_enemyFish = [[NSMutableArray alloc]init];
Then I add my enemy to the array like this:
[self addChild:yellowFish z:50];
[_enemyFish addObject:yellowFish];
I also remove it from the array like this:
CCMoveTo *move = [CCMoveTo actionWithDuration:speed position:ccp([login to view URL], randY)];
CCCallBlockN *done = [CCCallBlockN actionWithBlock:^(CCNode *node){
[_enemyFish removeObject:node];
[node removeFromParentAndCleanup:YES];}];
[blueFish runAction:[CCSequence actions:move,done, nil]];
Then in my update method I check for collisions:
-(void)update:(ccTime)dt
{
for (CCSprite *enemy in _enemyFish) {
if ([self collisionWithPoint:[login to view URL] andRadius:[login to view URL] * .3 andPoint:[login to view URL] andRadius:[login to view URL] * .4]) {
[self gameOver];
}
}
}
However, my issue is that my player will randomly die (gameOver scene being called) without being anywhere near the enemy. This only happens randomly though which confuses me. I have debugged with multiple CCLog statements to determine that this is indeed the issue. Does anyone have any idea why I would randomly be “colliding” with nothing based off of my given code? I can provide other code if needed.
Thank you!