swift - Easy SpriteKit 接触检测(太空射击游戏)无法正常工作

标签 swift sprite-kit xcode6 collision-detection uint32

我正在尝试制作一款简单的太空射击游戏。接触应该发生在鱼雷和外星人之间,或者航天飞机和外星人之间。问题是第二次接触(航天飞机与外星人)只发生在第一种接触(鱼雷与外星人)发生之后,而且它们并不总是精确的。这是在类外创建的结构

struct PhysicsCategory {
static let alien : UInt32 = 1
static let torpedo : UInt32 = 2
static let shuttle : UInt32 = 3 }

类车:

shuttle.physicsBody = SKPhysicsBody(rectangleOfSize: shuttle.size)
shuttle.physicsBody?.categoryBitMask = PhysicsCategory.shuttle
shuttle.physicsBody?.contactTestBitMask = PhysicsCategory.alien 
shuttle.physicsBody?.dynamic = false 
shuttle.physicsBody?.affectedByGravity = false

鱼雷:

torpedo.physicsBody = SKPhysicsBody(rectangleOfSize: torpedo.size)
torpedo.physicsBody?.categoryBitMask = PhysicsCategory.torpedo
torpedo.physicsBody?.contactTestBitMask = PhysicsCategory.alien
torpedo.physicsBody?.affectedByGravity = false
torpedo.physicsBody?.dynamic = false

外星人:

alien.physicsBody = SKPhysicsBody(rectangleOfSize: torpedo.size)
alien.physicsBody?.categoryBitMask = PhysicsCategory.alien
alien.physicsBody?.contactTestBitMask = PhysicsCategory.torpedo
alien.physicsBody?.affectedByGravity = false
alien.physicsBody?.dynamic = true

最后,这是我的联系方式:

    func didBeginContact(contact: SKPhysicsContact) {
    var firstBody : SKPhysicsBody = contact.bodyA
    var secondBody : SKPhysicsBody = contact.bodyB

    if ((firstBody.categoryBitMask == PhysicsCategory.alien) && (secondBody.categoryBitMask == PhysicsCategory.torpedo)) ||
    ((firstBody.categoryBitMask == PhysicsCategory.torpedo) && (secondBody.categoryBitMask == PhysicsCategory.alien)) {
        self.contactWithTorpedo(firstBody.node as! SKSpriteNode, torpedo: secondBody.node as! SKSpriteNode)
    } else if ((firstBody.categoryBitMask == PhysicsCategory.shuttle) && (secondBody.categoryBitMask == PhysicsCategory.alien)) {
            self.contactWithShuttle(firstBody.node as! SKSpriteNode, shuttle: secondBody.node as! SKSpriteNode)
    }
}


func contactWithTorpedo (alien: SKSpriteNode, torpedo : SKSpriteNode) {
    alien.removeFromParent()
    torpedo.removeFromParent()
    score++
    scoreLabel.text = "score: " + "\(score)"
}

func contactWithShuttle (alien:SKSpriteNode, shuttle:SKSpriteNode) {
    alien.removeFromParent()
    shuttle.removeFromParent()

    self.view?.presentScene(EndScene())

}

我不太确定问题出在哪里,而且我看到一些教程也有同样的问题。顺便说一句,我不知道它是否相关,但这不是 iOS 游戏而是 OSX 游戏。提前致谢!

最佳答案

您可能会发现按如下方式重组您的 didBeginContact 不会那么困惑,因为这避免了 firstBody/secondbody 的东西和复杂的 if...then 条件来查看什么联系过什么:

func didBeginContact(contact: SKPhysicsContact) {
    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch contactMask {
    case PhysicsCategory.alien | PhysicsCategory.torpedo:
       // alien and torpedo have contacted
       contact.bodyA.removeFromParent()
       contact.bodyB.removeFromParent()
       score += 1
       scoreLabel.text = "score: " + "\(score)"

    case PhysicsCategory.alien | PhysicsCategory.shuttle:
       // alien and shuttle have contacted
       contact.bodyA.removeFromParent()
       contact.bodyB.removeFromParent()

       self.view?.presentScene(EndScene())

    default :
        //Some other contact has occurred
        print("Some other contact")
    }
}

你可以添加尽可能多的 PhysicsCategory.enemy | PhysicsCategory.player 案例,因为您需要在游戏中采取行动的所有联系人。为每个潜在联系人单独编码,您不会陷入如果……那么……否则的困境。

如果您只需要引用接触中涉及的节点之一(例如,在敌人击中玩家后移除玩家),您可以这样做:

let playerNode = contact.bodyA.categoryBitMask == PhysicsCategory.player ? contact.bodyA.node! : contact.bodyB.node!
playernode.removefromParent

关于swift - Easy SpriteKit 接触检测(太空射击游戏)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36656396/

相关文章:

ios - 如何区分UITableViewCell中哪个项目被选中

objective-c - 如何使用 Sprite Kit 逐渐模糊 SKSpriteNode 的图像?

objective-c - 使用 spritekit,如何在横向卷轴游戏中实现滚动、随机倾斜的地板?

swift - xcode 6 中的选项卡栏 Controller 灰色框

swift - 为什么日期格式化程序正确返回日期但时间显示为 00 :00:00 when converting string to NSDate

ios - 构建 TensorFlowLite Swift 自定义框架

ios - 如何让TableViewCell阴影移出tableview

swift - 在 GameViewController 中调用 GameScene 的一个函数

ios - 添加选项卡栏 Controller 后不会触发 Unwind segue 和 nav 按钮项目

ios - UICollectionView 中的 Alamofire + Swift