当敌人靠近时,玩家不会发现碰撞

时间:2017-01-21 08:05:25

标签: swift sprite-kit collision-detection

我设法在子弹和敌人之间建立联系,但由于某种原因,主角不会与敌人接触。

    func didBegin(_ contact: SKPhysicsContact) {
    var firstBody:SKPhysicsBody
    var secondBody:SKPhysicsBody

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

    if (firstBody.categoryBitMask & Bullet) != 0 && (secondBody.categoryBitMask & Enemy) != 0{

        bulletDidCollideWithEnemy(bulletNode: firstBody.node as! SKSpriteNode, Zombie: secondBody.node as! SKSpriteNode)

    }else{
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }
    if (firstBody.categoryBitMask & Player) != 0 && (secondBody.categoryBitMask & Enemy) != 0{

        playerCollideWithEnemy(playerNode: firstBody.node as! SKSpriteNode, Zombie: secondBody.node as! SKSpriteNode)

        }

}

1 个答案:

答案 0 :(得分:0)

对于遇到与我一样的问题的人来说,这是解决问题的方法。我只需要确保&#34; Enemy&#34;其他对象可见。非常重要!希望这可以帮助别人。

Enemy.physicsBody?.affectedByGravity = false
Enemy.physicsBody?.isDynamic = true