SpriteKit account randomly

I am creating a game with SpriteKit and trying to increase the score in a collision. For some odd reason, every time the count increases, this random number is not only 1.

In the method didBeginContact, I have a collision between a bullet and an alien. Every time I came across, I would like the score to increase the score by 1. Except that he has his own mind and will randomly increase it by a number from 1 to 6. I went through each line of code and tried to add by scoring in the game several times, but it just does not increase by 1, as I would like. At the moment, my code is as follows.

In my header file, I create a property to evaluate:

@property (nonatomic) int score;

Then in my main game scene I add the following

-(void)setScore:(int)score
{
    _score = score;
    _scoreLabel.text = [NSString stringWithFormat:@"%d", score];
}

init , 0

self.score = 0;

, ​​ 0

self.score = 0;

. self.score ++;

if (firstBody.categoryBitMask == kAlienCategory && secondBody.categoryBitMask == kBulletCategory) {
        //Collision Between Bullet & Alien
        self.score++;
        if (firstBody.node) {
            [self alienExplosion:firstBody.node.position];
            [firstBody.node removeFromParent];
        }
        if (secondBody.node) {
            [self bulletExplosion:secondBody.node.position];
            [secondBody.node removeFromParent];
        }
    }

UPDATE: NSLog, , . , 1 - 6. 6, 1

enter image description here

enter image description here

+4
1

Box2D ( , SpriteKit ) .

, , BeginContact .

. . , BOOL Alive Alien .

//Collision Between Bullet & Alien
if (firstBody.categoryBitMask == kAlienCategory && secondBody.categoryBitMask == kBulletCategory) {
    Alien * alien = nil;
    if ([firstBody.node isKindOfClass:Alien.class]) {
        alien = (Alien*)firstBody.node 
    } else if ([secondBody.node isKindOfClass:Alien.class]) {
        alien = (Alien*)secondBody.node 
    }

    if (alien && alien.isAlive) {
        alien.isAlive = NO
        self.score++;
        if (firstBody.node) {
            [self alienExplosion:firstBody.node.position];
            [firstBody.node removeFromParent];
        }
        if (secondBody.node) {
            [self bulletExplosion:secondBody.node.position];
            [secondBody.node removeFromParent];
        }
    }
}
+3

Source: https://habr.com/ru/post/1570744/


All Articles