打开可选项时意外发现nil - EXC_BAD_Instruction

时间:2015-07-27 02:56:06

标签: objective-c swift optional

我正在尝试在Swift中实现一些冲突,无论如何,我似乎都会遇到EXC_BAD_INSTRUCTION严重错误。

我有

let heroCategory: UInt32 = 0x1 << 0
let bellCategory: UInt32 = 0x1 << 1

声明我的类的OUTSIDE(在类中声明时出现相同的错误)。

相关节点特征:

 bell.physicsBody = SKPhysicsBody(texture: bell.texture, size: bell.size)
    bell.physicsBody?.dynamic = true
    bell.physicsBody?.categoryBitMask = bellCategory
    bell.physicsBody?.contactTestBitMask = heroCategory
    bell.physicsBody?.collisionBitMask = heroCategory

    bell.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) - runningbar.size.height - rope.size.height - (bell.size.height / 2))


    hero.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(hero.size.width) / 2)
    hero.physicsBody?.affectedByGravity = true
    hero.physicsBody?.dynamic = true
    hero.physicsBody?.usesPreciseCollisionDetection = true
    heroBaseline = runningbar.position.y + (runningbar.size.height / 2) + (hero.size.height / 2)
    hero.position = CGPointMake(CGRectGetMinX(self.frame) + 200, self.heroBaseline)
    hero.physicsBody?.categoryBitMask = heroCategory
    hero.physicsBody?.contactTestBitMask = bellCategory
    hero.physicsBody?.collisionBitMask = bellCategory

我也有:

func didBeginContact(contact: SKPhysicsContact) {

    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {

        firstBody = contact.bodyA
        secondBody = contact.bodyB

    }else{

        firstBody = contact.bodyB
        secondBody = contact.bodyA

    }


    if firstBody.categoryBitMask == heroCategory && secondBody.categoryBitMask == bellCategory {

        println("test")

    }


   }

我在这行代码中遇到了这个特定的错误:

if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)

据我所知,当条件只能比较非零值时,它试图告诉我bodyA和bodyB返回的值为nil。但是,我无法理解在这种特定情况下发生的事情。

旁注:还有其他节点也会联系英雄,但我想要实际产生影响的唯一节点就是铃声,正如我在第二个条件声明中指出的那样。

0 个答案:

没有答案