SpriteNode之间没有碰撞检测

时间:2018-11-05 01:48:28

标签: swift collision detection

我有2个类别

enum ColliderType:UInt32 {
case bulletCategory = 1
case bossCategory = 2
} 

我让班级使用SKPhysicsContactDelegate并创建了一个节点

class PlayScene: SKScene, SKPhysicsContactDelegate {

var charBullets :[SKSpriteNode] = [SKSpriteNode]()
var charBulletTexture = SKTexture(imageNamed: "Bullet1.png")
var boss = SKSpriteNode()
var bossTexture = SKTexture(imageNamed: "BossImage.png")

并设置physicsWorld.contactDelegate并在didMove(以查看:SKView)函数中创建一个节点

override func didMove(to view: SKView) { 

physicsWorld.contactDelegate = self

boss.name = "boss"
boss = SKSpriteNode(texture: bossTexture)
boss.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 300, height: 300))
boss.physicsBody?.usesPreciseCollisionDetection = true
boss.physicsBody!.isDynamic = true
boss.physicsBody!.affectedByGravity = false
boss.size = CGSize(width: 300, height: 300)
boss.physicsBody!.categoryBitMask = ColliderType.bossCategory.rawValue
boss.physicsBody!.collisionBitMask = 0
boss.physicsBody!.contactTestBitMask = ColliderType.bulletCategory.rawValue
boss.position = CGPoint(x: 0, y: self.size.height/4)
addChild(boss)

}

我在touchesBegan函数中创建了另一个节点

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

charBullet.name = "bullet"
charBullet.physicsBody = SKPhysicsBody(circleOfRadius: charBullet.size.width/64)
charBullet.physicsBody!.isDynamic = true
charBullet.physicsBody!.usesPreciseCollisionDetection = true
charBullet.physicsBody!.affectedByGravity = false
charBullet.physicsBody!.velocity = CGVector.init(dx: 0, dy: 450)
charBullet.physicsBody!.categoryBitMask = ColliderType.bulletCategory.rawValue
charBullet.physicsBody!.collisionBitMask = 0
charBullet.physicsBody!.contactTestBitMask = ColliderType.bossCategory.rawValue
charBullet.position = CGPoint(x: character.position.x, y: character.position.y + 100)
addChild(charBullet)
charBullets.append(charBullet)

}

然后我有一个didBegin(_联系人:SKPhysicsContact),但是当子弹和上司发生碰撞时不会被调用

func didBegin(_ contact: SKPhysicsContact) {

    //not printed
    print("contact!")

    if(charBullets.count > 0) {
        var firstBody = SKPhysicsBody()
        var secondBody = SKPhysicsBody()

        if(contact.bodyA.node?.name == "bullet") {
            print("test1")
            firstBody = contact.bodyA
            secondBody = contact.bodyB
        } else {
            print("test2")
            firstBody = contact.bodyB
            secondBody = contact.bodyA
            print(secondBody)
        }

        if(firstBody.node?.name == "bullet") {
            print("bulletname")
        }

        if(firstBody.node?.name == "boss") {
            print("bossname")
        }

        if(secondBody.node?.name == "bullet") {
            print("bulletname2")
        }

        if(secondBody.node?.name == "boss") {
            print("bossname2")
        }

        if(firstBody.node?.name == "bullet" && secondBody.node?.name == "boss") {
                print("hit the boss!")
                charBullets[i].removeFromParent()
                bossHealth -= 1
                bossHealthLabel.text = "Boss Health \(bossHealth)"
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

更新:我通过更改其PhysicsBodys使其使用图像纹理大小(而不是设置值)并通过使用以下方式在节点创建下设置其名称来对其进行修复:

boss = SKSpriteNode(texture: bossTexture)
boss.name = "boss"
boss.physicsBody = SKPhysicsBody(texture: bossTexture, size: bossTexture.size())

let charBullet = SKSpriteNode(texture: charBulletTexture)
charBullet.name = "bullet"
charBullet.physicsBody = SKPhysicsBody(texture: charBulletTexture, size: charBulletTexture.size())