spritekit didBeginContact three object not worked

空扰寡人 提交于 2019-12-06 21:50:45

1) Based on your contact handler, you should set the categories to be

    let whiteCategory:  UInt32 = 0x1 << 0
    let blackCategory:  UInt32 = 0x1 << 1
    let blueCategory:   UInt32 = 0x1 << 2

2) You are incorrectly setting the blackSquare's physicsBody's bit masks twice

    blackSquare.physicsBody?.categoryBitMask = blueCategory
    blackSquare.physicsBody?.contactTestBitMask = whiteCategory

The above should be

    blueSquare.physicsBody?.categoryBitMask = blueCategory
    blueSquare.physicsBody?.contactTestBitMask = whiteCategory

3) The third contact body is not needed here.

func didBeginContact(contact: SKPhysicsContact!) {

    var firstBody, 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 & whiteCategory) != 0 &&
        (secondBody.categoryBitMask & blackCategory != 0)) {
            //secondBody.node?.removeFromParent()
            println("black")
    }


    if ((firstBody.categoryBitMask & whiteCategory != 0) &&
        (secondBody.categoryBitMask & blueCategory != 0)) {
            //secondBody.node?.removeFromParent()
            println("blue")
    }
}

4) The following is not needed (in touchesBegan). Setting the contact bit masks for the blue and black nodes is sufficient.

    whiteRectangle.physicsBody?.contactTestBitMask = blackCategory
    whiteRectangle.physicsBody?.contactTestBitMask = blueCategory
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!