0

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
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Mixstah
  • 411
  • 1
  • 7
  • 22
  • #LearnCocos2D what did you edit? – Mixstah Jul 20 '14 at 20:52
  • 1
    Sounds like the projectile and monster parameters are not always what you 'expect' them to be. Where does that method get called ? Post that code. The method you have is not making any determination of which is which, it makes an assumption that you are passing the correct objects to match the parameter, right ? – prototypical Jul 20 '14 at 22:59
  • How do you call the didCollideWithMonster method? – ZeMoon Jul 21 '14 at 05:52

1 Answers1

0

It seems to me that you are not sorting the bodies that you receive as parameters in the -didBeginContact method.

The bodyA and bodyB properties of the contact object do no necessarily contain the same type of bodies each time the contact delegate is called. In your case, contact.bodyA as well as contact.bodyB can both contain either the projectile or the monster node.

You can sort the bodies as follows:

-(void)didBeginContact:(SKPhysicsContact *)contact`
{

    NSLog(@"contact detected");

    SKPhysicsBody *firstBody;
    SKPhysicsBody *secondBody;

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    {
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }

    //Implement relevant code here.

}

In the above code, you can call the -didCollideWithMonster by passing firstBody and secondBody. Of the two bitmask categories involved, the one with the lower vaue will be firstBody and the one with a higher value will be secondBody.

Please have a look at this answer as well.

Community
  • 1
  • 1
ZeMoon
  • 20,054
  • 5
  • 57
  • 98