Hi I am having trouble using collision detection. What's happening is when the projectile (bullet) hits an alien a particle spark is made. Trouble is sometimes I think that this function gets the projectile and monster mixed up so sometimes it generates the spark where the bullet is destroyed.
Q. What determines which sprite hits which.
Can anyone offer any advice?
- (void)projectile:(SKSpriteNode *)projectile didCollideWithMonster:(SKSpriteNode *)monster
{
[projectile removeFromParent];
canfire = true;
NSString *path = [[NSBundle mainBundle]pathForResource:@"spark" ofType:@"sks"];
SKEmitterNode *spark = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
spark.position = monster.position;
SKAction *pause = [SKAction waitForDuration:1.0];
SKAction *remove = [SKAction removeFromParent];
[spark runAction:[SKAction sequence:@[pause,remove]]];
spark.position = monster.position;
[monster addChild:spark];
[monster removeFromParent];
}
Yes definitely getting the projectile and monster mixed up:
2014-07-20 19:56:35.279 Space Warz[9420:60b] First Body name = bullet
2014-07-20 19:56:35.280 Space Warz[9420:60b] Hit
2014-07-20 19:56:35.281 Space Warz[9420:60b] Monster Name: crystalship
2014-07-20 19:56:40.878 Space Warz[9420:60b] Contact
2014-07-20 19:56:40.879 Space Warz[9420:60b] First Body name = crystalship
2014-07-20 19:56:40.880 Space Warz[9420:60b] Hit
2014-07-20 19:56:40.881 Space Warz[9420:60b] Monster Name: bullet
2014-07-20 19:56:43.845 Space Warz[9420:60b] Contact
2014-07-20 19:56:43.846 Space Warz[9420:60b] First Body name = bullet
2014-07-20 19:56:43.847 Space Warz[9420:60b] Hit
2014-07-20 19:56:43.848 Space Warz[9420:60b] Monster Name: crystalship
2014-07-20 19:56:43.855 Space Warz[9420:60b] Contact
2014-07-20 19:56:43.856 Space Warz[9420:60b] First Body name = bullet
2014-07-20 19:56:43.857 Space Warz[9420:60b] Hit
2014-07-20 19:56:43.858 Space Warz[9420:60b] Monster Name: crystalship