SpriteKit / Swift - How to check contact of two nodes when they are already in contact

好久不见. 提交于 2019-12-04 19:36:08

I've just tried to make two sprites overlap from the very beginning and contact is detected for me. Here is the code:

import SpriteKit

struct Collider {
    static let SmallSquare     : UInt32 = 1 << 0
    static let BigSquare       : UInt32 = 1 << 1
}

class GameScene: SKScene, SKPhysicsContactDelegate {

    override func didMoveToView(view: SKView) {

        self.physicsWorld.contactDelegate = self

        let smallSquare = SKSpriteNode(color: .orangeColor(), size: CGSize(width: 50, height:50))
        smallSquare.zPosition = 2
        smallSquare.position = CGPoint(x: frame.midX, y: frame.midY)
        smallSquare.physicsBody = SKPhysicsBody(rectangleOfSize: smallSquare.size)
        smallSquare.physicsBody?.affectedByGravity = false
        smallSquare.physicsBody?.categoryBitMask = Collider.SmallSquare
        smallSquare.physicsBody?.contactTestBitMask = Collider.BigSquare
        smallSquare.physicsBody?.collisionBitMask = 0
        addChild(smallSquare)

        let bigSquare = SKSpriteNode(color: .purpleColor(), size: CGSize(width: 200, height: 200))
        bigSquare.zPosition = 1
        bigSquare.position = CGPoint(x: frame.midX, y: frame.midY)
        bigSquare.physicsBody = SKPhysicsBody(rectangleOfSize: bigSquare.size)
        bigSquare.physicsBody?.affectedByGravity = false
        bigSquare.physicsBody?.categoryBitMask = Collider.BigSquare
        bigSquare.physicsBody?.contactTestBitMask = Collider.SmallSquare
        bigSquare.physicsBody?.collisionBitMask = 0
        addChild(bigSquare)

    }

    func didBeginContact(contact: SKPhysicsContact) {

        print("Contact detected")
    }    
}

Later on, to appropriately detect contact between certain bodies, you should do something like this:

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 & Collider.SmallSquare) != 0 &&
            (secondBody.categoryBitMask & Collider.BigSquare != 0)) {

                print ("Contact detected")
        }

    }

But even without that, the message will be printed when game is started for the first time because contact is detected by physics world anyways.

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