0

I have problem when i updating score in the game i make i dont know what is the problem but when i shoot something the score should increment by 1 but in my code sometimes it increment by 2 or 3 sometimes 1 it not constant i dont know why this happen here is the code i used

@interface GameScene () {

    SKLabelNode* _scoreLabelNode;
    NSInteger _score;


}


-(void)didMoveToView:(SKView *)view {

    _score = 0;
        _scoreLabelNode = [SKLabelNode labelNodeWithFontNamed:@"Silom Regular"];
        _scoreLabelNode.fontSize = 50;
        _scoreLabelNode.position = CGPointMake(self.size.width - 335 , self.size.height - 60);
        _scoreLabelNode.zPosition = 100;
        [self addChild:_scoreLabelNode];

        _scoreLabelNode.text = [NSString stringWithFormat:@"%d",_score];
}

if (contact.bodyB.categoryBitMask == ObjectCategory) {

        _score++;
        _scoreLabelNode.text = [NSString stringWithFormat:@"%d",_score];
}
RUON
  • 163
  • 9
  • add breakpoint on _score++ , just to check how many times it's getting incremented – Misha Aug 12 '15 at 09:46
  • Where did you write this if condition `if (contact.bodyB.categoryBitMask == ObjectCategory)` ? on which event ? – NeverHopeless Aug 12 '15 at 09:52
  • See if this helps : http://stackoverflow.com/a/28527280/3402095 If nothing from that post can't be applied to your code, you can try with something like this : http://stackoverflow.com/a/23367987/3402095 – Whirlwind Aug 12 '15 at 09:54
  • @NeverHopeless in this -(void)didBeginContact:(SKPhysicsContact *)contact { – RUON Aug 12 '15 at 10:00
  • @Misha i put "NSLog (@"%d", _score )" after "_score++" it increment as should like 10 11 12 .. etc but the score jumped from 10 to 12 – RUON Aug 12 '15 at 10:08

1 Answers1

1

There are few similar issue with this delegate, see if this may fix your issue:

SpriteKit: didBeginContact being called non-stop on iPad

Why are didBeginContact called multiple times?

didBeginContact is being called multiple times for the same SKPhysicsBody

Even if it doesn't solve your problem, you can use a flag variable to handle this score update for once. e.g.,

bool hasScoreUpdated;

- (void)didBeginContact:(SKPhysicsContact * _Nonnull)contact
{
    if(!hasScoreUpdated)
    {
        _score++;
        hasScoreUpdated = true;
    }
     // your rest of the logic
}

- (void)didEndContact:(SKPhysicsContact * _Nonnull)contact
{
    hasScoreUpdated = false;
}

EDIT:

Based on your comment above:

i put "NSLog (@"%d", _score )" after "_score++" it increment as should like 10 11 12 .. etc but the score jumped from 10 to 12

It is possibly due to the very frequently calling the respective event and a very rapidly update of UI element.

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • 1
    thank u but the link "Why are didBeginContact called multiple times?"above broken – RUON Aug 12 '15 at 10:23
  • can i ask you something outside this question , i want to set the touch only in specific area in the scene do you know how i can do that ? – RUON Aug 13 '15 at 07:08
  • Look at this thread: http://stackoverflow.com/questions/26656896/set-touchable-area-of-skspritenode – NeverHopeless Aug 13 '15 at 07:10