为 SKSpriteNode 创建物理 [​​=10=] 以检测接触

creating physics body for a SKSpriteNode in order to detect contact

如标​​题所示,我无法在我的敌人精灵和英雄激光(子弹)之间建立联系。我通过以下方法创建我的敌人并将它们添加到视图中。

-(void)Enemies{
//not always come
int GoOrNot = [self getRandomNumberBetween:0 to:1];

if(GoOrNot == 1){

    SKSpriteNode *enemy;

    int randomEnemy = [self getRandomNumberBetween:0 to:1];
    if(randomEnemy == 0)
        enemy = [SKSpriteNode spriteNodeWithImageNamed:@"enemy_spaceship.png"];
    else
        enemy = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship15.png"];


    enemy.scale = 0.4;

    enemy.position = CGPointMake(450, 0);
    enemy.hidden = NO;

    CGPoint location = CGPointMake(450, 320);

    SKAction *moveAction = [SKAction moveTo:location duration:2.5];
    SKAction *doneAction = [SKAction runBlock:(dispatch_block_t)^() {
        //NSLog(@"Animation Completed");
        enemy.hidden = YES;
    }];

    SKAction *moveAsteroidActionWithDone = [SKAction sequence:@[moveAction,doneAction ]];

    [enemy runAction:moveAsteroidActionWithDone withKey:@"asteroidMoving"];
    [self addChild:enemy];

    _enemyBullets = [[NSMutableArray alloc] initWithCapacity:kNumEnemyBullet];
    for (int i = 0; i < kNumEnemyBullet; ++i) {
        SKSpriteNode *enemyBullets = [SKSpriteNode spriteNodeWithImageNamed:@"rocket"];
        enemyBullets.hidden = YES;
        [enemyBullets setXScale:0.5];
        [enemyBullets setYScale:0.5];
        [_enemyBullets addObject:enemyBullets];
        [enemy addChild:enemyBullets];
    }

}

}

然后我通过可变数组添加子弹,然后将我的子弹添加到敌人精灵中作为他们的 child。那部分有效。我可以在英雄和敌人的子弹之间建立联系,并且它会被检测到。我的问题是我英雄的激光不会与敌人产生接触,因此我无法让敌人被激光击中。我尝试将物理 body 添加到我正在使用的方法中,但将所有其他 sprite 扔进了地狱,它们不再正确响应。

以下代码是我在更新方法中使用的碰撞代码:

for (SKSpriteNode *enemyBullets in _enemyBullets) {
        if (enemyBullets.hidden) {
            continue;
        }
        if ([_ship intersectsNode:enemyBullets]) {
            enemyBullets.hidden = YES;
            SKAction *blink = [SKAction sequence:@[[SKAction fadeOutWithDuration:0.1],
                                                   [SKAction fadeInWithDuration:0.1]]];
            SKAction *blinkForTime = [SKAction repeatAction:blink count:4];
            SKAction *shipExplosionSound = [SKAction playSoundFileNamed:@"explosion_large.caf" waitForCompletion:NO];
            [_ship runAction:[SKAction sequence:@[shipExplosionSound,blinkForTime]]];
            _lives--;
            _livesLabel.text = [NSString stringWithFormat:@"%d Lives", _lives];
            NSLog(@"your ship has been hit!");
        }
    }

有什么方法可以用来为我的敌人创建物理 [​​=21=],这样我就可以在英雄激光和敌人精灵之间创建与我目前拥有的结构相同的接触?非常感谢任何帮助。

您当然可以为您的敌人创建一个物理系统 body。在此代码 enemy = [SKSpriteNode spriteNodeWithImageNamed:@"enemy_spaceship.png"]; 之后,您可以像这样添加物理 body:

enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
enemy.physicsBody.categoryBitMask = CategoryEnemy; // or whatever you need
enemy.physicsBody.collisionBitMask = CategoryRock; // or whatever you need
enemy.physicsBody.contactTestBitMask = CategoryBullet // or whatever you need
enemy.physicsBody.dynamic = NO; // or YES
enemy.physicsBody.allowsRotation = NO; // or YES
enemy.physicsBody.restitution = 0; // or another value between 0 to 1
enemy.physicsBody.friction = 0.0; // or another value between 0 to 1

创建物理的方法有很多种body。您可以画矩形、圆形、勾勒纹理或自己绘制。

您应该阅读有关此主题的 Apple docs 以更好地了解您可以做什么以及可以使用哪些属性。