didBeginContact()在没有联系人的情况下调用

时间:2016-01-08 17:36:39

标签: ios swift sprite-kit swift2 skphysicsbody

我有一个准备发布的游戏,但我仍然在iOS 8.1上的未婚夫有这个问题。它适用于iOS 9,但我在iOS 8.1的模拟器上重新创建了这个问题。

当我点击开始游戏并加载场景时,敌人开始出现在屏幕的边缘。他们使用动作将它们推到屏幕的另一边,然后将它们从父节点中删除。只有,didBeginContact()在被添加到屏幕后立即被调用。

struct PhysicsCategory {
    static let None     : UInt32 = 0
    static let All      : UInt32 = UInt32.max
    static let fishy    : UInt32 = 0b1       // 1
    static let enemy    : UInt32 = 0b10      // 2
    static let powerup    : UInt32 = 0b11      // 3
}

好人(腥)设置和运动)

fishy.physicsBody = SKPhysicsBody(texture:fishy.texture!, size:fishy.size)
    fishy.physicsBody?.dynamic = true
    fishy.physicsBody?.categoryBitMask = PhysicsCategory.fishy
    fishy.physicsBody?.contactTestBitMask = PhysicsCategory.enemy
    fishy.physicsBody?.collisionBitMask = PhysicsCategory.None
    fishy.physicsBody?.usesPreciseCollisionDetection = true

敌人设置和移动

enemy.physicsBody = SKPhysicsBody(texture:enemy.texture!, size:enemy.size)
    enemy.physicsBody?.dynamic = true // 2
    enemy.physicsBody?.categoryBitMask = PhysicsCategory.enemy // 3
    enemy.physicsBody?.contactTestBitMask = PhysicsCategory.fishy // 4
    enemy.physicsBody?.collisionBitMask = PhysicsCategory.None // 5

self.addChild(enemy)
    let action1 = SKAction.rotateToAngle(angle, duration: 0.0, shortestUnitArc: true)
    let action = SKAction.moveTo(finishPos,duration:Double(random(min: 5,max: 13)))
    let action2 = SKAction.removeFromParent()
    let sequence = SKAction.sequence([action1,action,action2])
enemy.runAction(sequence)

这是针对添加的大多数敌人节点调用的,但不是全部。我只想在他们联系时调用它,但是它在创建时运行。 (即使敌人的路径与腥鱼不接触,它也会呼唤)

func didBeginContact(contact: SKPhysicsContact) {

    var enemyContact: SKPhysicsBody
    var fishyContact: SKPhysicsBody
    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        fishyContact = contact.bodyA
        enemyContact = contact.bodyB
    } else {
        fishyContact = contact.bodyB
        enemyContact = contact.bodyA
    }
    if ((enemyContact.categoryBitMask & PhysicsCategory.enemy != 0) && (fishyContact.categoryBitMask & PhysicsCategory.fishy != 0)) {
        if (enemyContact.node != nil && fishyContact.node != nil) {
                projectileDidCollide(fishyContact.node as! SKSpriteNode, badGuy:enemyContact.node as! SKSpriteNode)
        }
    }
}

同样,这在iOS 9.0及更高版本中不会发生。提前谢谢。

0 个答案:

没有答案