代码仅适用于 iOS 7

Code only working on iOS 7

您好,我一直在使用 SpriteKit 框架制作游戏,当两个对象发生碰撞时,我设置了一个碰撞位掩码。其中一个对象,比如说对象 A,可以有两种状态:黑色或正常,所以当两个对象发生碰撞并且 A 对象处于正常状态时,它会添加一个点,但是当两个对象发生碰撞并且 A 对象处于黑色时表示游戏结束。这段代码在 iOS 7 上对我来说工作正常,但是当我在 iOS 8 上 运行 并且如果 A 对象状态为黑色时,它的行为就像它处于正常状态并添加一个点.为什么会这样? iOS 7 和 8 的代码不同吗?请任何人帮助我,这是代码:

-(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]]]];

    }

}

为了设置状态,我在添加对象 A 的方法中使用了这段代码:

  // 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;
 }

我终于解决了这个问题。首先,我必须创建 collision 变量:

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

然后用这个写碰撞:

-(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];

    }
}