0

Hi I've been making a game with the SpriteKit framework and I set a collision bit mask when 2 objects collide. One of those objects, let's say the object A, can have 2 states: black or normal so when the two objects collide and the A object is on normal state it adds a point but when the two objects collide and the A object is on black state the game is over. This code is working fine for me on iOS 7 but when I run it on iOS 8 and if the A object state is black it acts like if it's on normal state and adds a point. Why this is happening? Is different the code for iOS 7 and 8? Please can anyone help me, here's the code:

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

SKPhysicsBody *firstBody;
SKPhysicsBody *secondBody;

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

   uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

if (firstBody.categoryBitMask == objectACategory && secondBody.categoryBitMask == objectBCategory) {
    NSLog(@"OBJECT A CAUGHT");
    [firstBody.node removeFromParent];
    [GameState sharedInstance].score++;
    _scoreLabel.text = [NSString stringWithFormat:@"%d", [GameState sharedInstance].score];
    _scoreLabel_2.text = [NSString stringWithFormat:@"%d", [GameState sharedInstance].score];

    gameUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound " ofType:@"wav"]];
    _gameSound = [[AVAudioPlayer alloc] initWithContentsOfURL:gameUrl error:nil];
    _gameSound.delegate = self;

    [_gameSound play];

    if ([[firstBody.node.userData valueForKey:@"Black"] boolValue]) {
        [self removeActionForKey:origamiFallKey];
        NSLog(@"YOU LOSE");
        [self gameOver];

        [self runAction:[SKAction sequence:@[
                                             [SKAction waitForDuration:0.5],
                                             [SKAction performSelector:@selector(removeWhenLose) onTarget:self]]]];

    }

}

For setting the state I used this code in the method which adds a objectA:

  // Black
 if(arc4random_uniform(6) == 0) {
 _objectA.texture = [SKTexture textureWithImageNamed:@"blackImage.png"];
  _objectA.physicsBody.collisionBitMask = objectACategory;
 _objectA.userData = [[NSMutableDictionary alloc] init];
 [_objectA.userData setValue:@YES forKey:@"Black"];
 blackObjectA = YES;
 }

1 Answers1

1

I finally solved this problem. First I had to create the collision variable:

uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

And with write the collisions using this:

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

if (collision == (objectACategory | objectBCategory)) {
    if ([[firstBody.node.userData valueForKey:@"Black"] boolValue]){
        [self removeActionForKey:fallKey];
        NSLog(@"YOU LOSE");
        [self gameOver];

        [self runAction:[SKAction sequence:@[
                                             [SKAction waitForDuration:0.5],
                                             [SKAction performSelector:@selector(removeWhenLose) onTarget:self]]]];
    } else {

        [firstBody.node removeFromParent];
        [GameState sharedInstance].score++;
        _scoreLabel.text = [NSString stringWithFormat:@"%d", [GameState sharedInstance].score];
        _scoreLabel_2.text = [NSString stringWithFormat:@"%d", [GameState sharedInstance].score];

        gameUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"origami " ofType:@"wav"]];
        _gameSound = [[AVAudioPlayer alloc] initWithContentsOfURL:gameUrl error:nil];
        _gameSound.delegate = self;

        [_gameSound play];

    }
}