使SKSprites注册冲突但不会相互影响

时间:2017-09-15 17:16:40

标签: ios swift3 collision

我想在我的游戏中添加加电,但是当我的精灵击中加油精灵时,它会让他偏离正常,因为游戏已经发生碰撞,我猜物理开始发挥作用,当我点击屏幕时,我的精灵跳到了空中,击中通电并停止向上移动并再次跌倒。

我需要精灵跳起来,收集上电精灵并且分数上升(分数上升代码完成)但是我无法弄清楚如何使2个精灵没有记录碰撞。

    //Create the PowerUp Coin to add score
func createCoin() {

    coinPlus5 = SKSpriteNode(imageNamed: "coin1")
    coinPlus5.name = "coinPlus5"
    coinPlus5.position.x = self.frame.size.width*2.6
    coinPlus5.position.y = CGFloat.randomBetweenNumbers(firstNum: -(self.frame.size.height / 2.8), secondNum: (self.frame.size.height / 2.4))
    coinPlus5.zPosition = 11
    coinPlus5.size = CGSize(width: 50, height: 50)
    hitPowerup = false

    // Add Physics Bodys
    coinPlus5.physicsBody = SKPhysicsBody(texture: coinPlus5.texture!, size: CGSize(width: coinPlus5.size.width, height: coinPlus5.size.height))


    //enemyBird1.physicsBody = SKPhysicsBody(circleOfRadius: self.size.height/13)
    coinPlus5.physicsBody?.allowsRotation = false
    coinPlus5.physicsBody?.affectedByGravity = false
    coinPlus5.physicsBody?.isDynamic = false
    // Add Collision Bitmasks
    coinPlus5.physicsBody?.categoryBitMask = ColliderType.Pipes

字符精灵代码是:

 func initialise() {

    self.name = "Bird"
    self.zPosition = 3
    self.size = CGSize(width: 130, height: 110)
    self.anchorPoint = CGPoint(x: 0.5, y: 0.5)






    self.physicsBody = SKPhysicsBody(texture: self.texture!,
                                     size: CGSize(width: self.size.width,
                                                  height: self.size.height))

    // Add Physics Bodys
    self.physicsBody?.allowsRotation = false
    self.physicsBody?.affectedByGravity = false
    self.physicsBody?.mass = CGFloat(-100)
    // Add Collision Bitmasks
    self.physicsBody?.categoryBitMask = ColliderType.Bird
    self.physicsBody?.collisionBitMask = ColliderType.Ground | ColliderType.Pipes
    self.physicsBody?.contactTestBitMask = ColliderType.Ground | ColliderType.Pipes | ColliderType.Score


}

1 个答案:

答案 0 :(得分:1)

目前您的硬币物理类别为ColliderType.Pipes。您已将鸟的collisionBitMask设置为与地面和管道(ColliderType.Ground | ColliderType.Pipes)发生碰撞。这意味着您的玩家将与硬币发生物理碰撞。

要解决这个问题,你可能想要一个新的碰撞器类型用于硬币。您希望硬币成为玩家contactTestBitMask的一部分,这样您就可以检测到玩家获得硬币的时间,但该玩家的一部分' s collisionBitMask所以他们不会影响你的物理。