I am working on a SpriteKit game where in one of the scenes I have to update score based on collision between two sprites. I intend to increment score by 1 whenever those two SKSpriteNodes collide with each other. There is a collision detection method in SpriteKit, which takes care of collision detection itself. I am using that default method didBeginContact: to detect collision, remove one of the objects involved in collision and increment score by 1. There are multiple objects of the same kind falling from the top and there is a basket like object that a player can move horizontally to catch those falling objects. On colliding with the basket, objects falling from the top are removed and score get incremented. The issue is very simple, that the score is being incremented not only by 1, but by 2,3,4,5 as well. That means instead of detecting single collision as it should be the case, it is detecting more than one collisions and hence incrementing score accordingly.
I have viewed here another question like this but that solution can never apply to mine. In my game for a limited time, similar objects keep falling from top until the time ends. Is there any possible way to solve this issue. I have tried using bool variable like didCollide inside collision detection method and even in a separate score incrementing method but the issue does not get resolved.
Here is the code I am trying in collision detection method.
-(void)didBeginContact:(SKPhysicsContact *)contact {
if (contact.bodyA.categoryBitMask == Stones) {
[contact.bodyA.node removeFromParent];
if (contactOccurred == NO) {
contactOccurred = YES;
[self updateScore:contactOccurred];
}
}
else {
[contact.bodyB.node removeFromParent];
if (contactOccurred == NO) {
ContactOccurred = YES;
[self updateScore:contactOccurred];
}
}
}
Code snippet for the method to increment score is here.
-(void)updateScore:(BOOL)collisionOccurred {
if (collisionOccurred == YES) {
contactOccurred = NO;
self.score= self.score + 1;
}
}