SKPhysicsBody没有名为“你的变量”的成员

时间:2014-10-18 16:28:43

标签: xcode swift sprite-kit skphysicsbody

我的代码中出现错误。我以为我打电话给contact.bodyA但到目前为止还没读过。我错过了什么或使用if语句都错了吗?我是一名新程序员,如果这非常容易或者含糊不清,那就很抱歉。如果您需要更多信息,请与我们联系!提前谢谢!

func didBeginContact(contact: SKPhysicsBody!){

    // Body1 and 2 depend on the categoryBitMask << 0 and << 1
    var firstBody:SKPhysicsBody
    var secondBody:SKPhysicsBody

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask){ ERROR- (SKPhysicsBody does not have a member named'bodyA')
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    }else{
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    if ((firstBody.contactTestBitMask & photonTorpedoCategory) != 0 && (secondBody.contactTestBitMask & alienCategory) != 0){
        torpedoDidCollideWithAlien(firstBody.node as SKSpriteNode, alien: secondBody.node as SKSpriteNode)
    }
}

2 个答案:

答案 0 :(得分:0)

这是因为联系不应该是SKPhysicsBody;它应该是SKPhysicsContact对象。 SKPhysicsBody没有名为bodyA(或bodyB)的成员,因为他们是SKPhysicsContact的成员。

didBeginContact函数的正确声明应如下所示:

func didBeginContact(contact: SKPhysicsContact) {
    //Your code in here
}

答案 1 :(得分:-2)

试试:

func didBeginContact(contact: SKPhysicsBodyContact!){

    // Body1 and 2 depend on the categoryBitMask << 0 and << 1
    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.contactTestBitMask & photonTorpedoCategory) != 0 && (secondBody.contactTestBitMask & alienCategory) != 0){
        torpedoDidCollideWithAlien(firstBody.node as SKSpriteNode, alien: secondBody.node as SKSpriteNode)
    }
}