三个精灵的didBeginContact适用于2个精灵,但不适用于第三个精灵--Swift2

时间:2015-11-05 18:03:52

标签: ios swift swift2

我有一只鸟,多张桌子和一颗星。当鸟与其中一个办公桌发生碰撞时,didBeginContact方法工作正常,但当它与星碰撞时,没有任何反应。我将复制代码,我添加了一些print语句,以便更容易看到发生了什么,然后我将粘贴它打印出来的打印语句。

Bird(只是physicsBody部分的初始化):

class Bird: SKSpriteNode {
    var sc: SKScene!

    init(sc: SKScene) {
        let texture = SKTexture(imageNamed: "hero")
        let size = texture.size()
        self.sc = sc
        super.init(texture: texture, color: UIColor.clearColor(), size: CGSizeMake(size.width/2, size.height/2))

        self.zPosition = Layer.Bird.rawValue
        self.name = "bird"

        self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/2)
        self.physicsBody?.categoryBitMask = PhysicsCategory.Bird
        self.physicsBody?.collisionBitMask = PhysicsCategory.Desk
        self.physicsBody?.contactTestBitMask = PhysicsCategory.Desk | PhysicsCategory.StarSpecial | PhysicsCategory.Star

        self.physicsBody?.allowsRotation = true
        self.physicsBody?.restitution = 0
        self.physicsBody?.usesPreciseCollisionDetection = true
   }

// other methods and the required init 
}

星级类的构造方式相同,因此我只需复制physicsBody相关代码:

    self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/2, center: self.position)
    self.physicsBody?.affectedByGravity = false
    self.physicsBody?.pinned = true

    self.physicsBody?.categoryBitMask = PhysicsCategory.StarSpecial
    self.physicsBody?.collisionBitMask = PhysicsCategory.None
    self.physicsBody?.contactTestBitMask = PhysicsCategory.Bird
    self.physicsBody?.usesPreciseCollisionDetection = true

    self.physicsBody?.allowsRotation = false
    self.physicsBody?.restitution = 0
    self.physicsBody?.usesPreciseCollisionDetection = true

didBeginContact中的GameScene方法:

func didBeginContact(contact: SKPhysicsContact) {
    print("something has collided")
    if contact.bodyA.categoryBitMask == PhysicsCategory.Bird && contact.bodyB.categoryBitMask == PhysicsCategory.Desk {
        birdSprite.BirdDidCollideWithDesk(contact.bodyA.node as! Bird, desk: contact.bodyB.node as! Desk, scoreClass: scoreClass, scoreLabel: scoreLabel)
        print("bird and desk have collided")
    } else if contact.bodyA.categoryBitMask == PhysicsCategory.Desk && contact.bodyB.categoryBitMask == PhysicsCategory.Bird  {
        print("desk and bird have collided")
        birdSprite.BirdDidCollideWithDesk(contact.bodyB.node as! Bird, desk: contact.bodyA.node as! Desk, scoreClass: scoreClass, scoreLabel: scoreLabel)
    }

    if contact.bodyA.categoryBitMask == PhysicsCategory.Bird && contact.bodyB.categoryBitMask == PhysicsCategory.StarSpecial {
        print("collided into star1")
        starSpecial.removeStar(contact.bodyB.node as! Star)
    } else if contact.bodyA.categoryBitMask == PhysicsCategory.StarSpecial && contact.bodyB.categoryBitMask == PhysicsCategory.Bird {
        print("collided into star2")
        starSpecial.removeStar(contact.bodyB.node as! Star)
    }
}

如果我运行它,我收到以下消息:

  1. 如果鸟与桌子发生碰撞:"something has collided""desk and bird have collided"
  2. 如果鸟与明星碰撞:"something has collided"
  3. 未满足后两个条件,因此不会打印出"collided into star1""collided into star2"消息。

    知道可能导致这种情况的原因是什么?

0 个答案:

没有答案