代码仅适用于 iOS 7



嗨,我一直在使用 SpriteKit 框架制作游戏,并在 2 个对象碰撞时设置了一个碰撞位掩码。其中一个对象,假设对象 A,可以有 2 种状态:黑色或正常,因此当两个对象碰撞并且 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];
    }
}

最新更新